• Advertisement

Archived

This topic is now archived and is closed to further replies.

Help with dynamic arrays

This topic is 6055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I''m trying to read the vertices of an object out of a 3DS file, and I''ve successfully located the data, but as I don''t know beforehand how vertices there will be, I wanted to create a dynamic array, that would size itself according to a value taken from the file. I''m writing in C++ but I''m still learning it, and, although I know how to do what I want in VB, I haven''t been able to find examples for C++. Could someone please either post a solution, or suggest a different way of reading in an unspecified amount of data from a file. Thanks $£¥ We scratch our eternal itch A twentieth century bitch We are grateful for Our Iron Lung

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You need basic knowledge of pointers for this.

From there, you simply create your pointer, as below:

int *example;

and then allocate memory to it:

example = new int[5]; // creates 5 ints

and after finishing with it, it''s always good to de-allocate any memory (to avoid leaks).

delete [] example;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Take a look at malloc and calloc, also if you are felling adventurous take a look at vectors

Malloc = allocate memory for an array , sequential = fast
Callco = the way the ms info says you should do the above.

Linked lists = memory can be all over the place so slower than arrays, but you can easily add to them.

Vector’s = no not the geom type, some guy from HP wrote the VC implementation I don’t know that much about there internal workings but they seem very cool, I have been told they are fast and you can add to them dynamically pushback I think. O also vertors are STD so they are implementation specific I think.

Hope this helps


Anon

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah this is a really cool thing to do as you don''t the size of an array at runtime.

int *example = new int[anysize];

Yes basically the new keyword is used to dynamically allocate one or more objects at runtime in an area of memory called the heap. It returns a pointer to the first element in an array.

Always rember to deallocate your dynamic arrays:

delete[] example;

If your new to C++ and have been coding in VB then you probubaly don''t know what a pointer is otherwise ignore this quick brief.

A pointer holds the address of another variable. It is said to ''point'' to the variable.

you can access the data in pointers by using the indirection opperator ie:

int a = 5;
int *example

// & means address of so example points to the address of the variable a.

example = &a;

// Then you can acess the data that is held in what is pointed to by example using the indirection opperator *.
*example = 6;

So now the value in a will be 6 instead of five.

Sorry if this is a bit basic but you need to know pointers in C++ they are vital and don''t exsist in most other programming languages

Happy Coding :-)

~Steve~

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You don''t need a dynamic array at all. Just make two passes through the data. The first pass you can find out how many verticies are there and then you can allocate your memory and then read the verticies in on your second pass.

Nate

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
The first pass you can find out how many verticies are there and then you can allocate your memory

... which would give you a dynamic array.



~~~~~~~~~~
Martee

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Dynamic can be interpreted two ways. First, simply an array that is allocated at runtime. Second, an array that is allocated at runtime and can resize itself.
I believe Nate meant dynamic in that it could resize itself. (using linked-lists you could allocate the memory as you read in data)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A dynamic array is an array that has the ability to change its size on the fly, for example a vector. Allocating an array of fixed size that never changes is hardly dynamic. It is a dynamic memory allocation, but not a dynamic array.

Nate

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi


Anyone have a good description of the internal workings of the Vector’s I presume they are a liked list of dynamically allocated memory, anyone like to enlighten me?


Anon

Share this post


Link to post
Share on other sites
No, actually Vectors are more like simple arrays than link lists. It allocates some set amount of memory, and then when you want more than it has, it has to allocate a larger block, and copy the old data over. That''s why they can get slow if they have to keep resizing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Pseudo: Are you sure about that ? This is more the working of the realloc() function. And it''s quite dangerous to use, if you want to avoid using double referenced pointers.

Eg.

int *a;
int *b;

a = realloc(NULL, 1024); // initial allocation
*a = 5;
b = a;

a = realloc(a, 2048); // We need more

printf("%d\n", *b);

We would expect a ''5'' here, but the result is likely to be a crash, since the memory may have been moved somewhere else and is invalid. That means, that you cannot keep _any_ pointers to this kind of memory, or you have to use things like **b=5 and update *b to the new pointer location. Confusing.

-AH

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Damn, how can you represent square brackets on this message board without triggering some kind of bizarre font effect ?

-AH

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Pseudo Thanks for the info but,

Are you sure, it seems to me that copying over the data would be a woeful waste of time and not to mention recourses. Granted I would expect that there would be over allocation each time the memory is allocated so that you are not copying over at each pushback (or whatever it is). After my last post I was thinking of ways to implement a vector type memory allocation method my self. It would need to be fast so I would like to shy away from linked lists as much as possible sequential array’s are faster (I am quite sure on that one). Anyway would not a linked list of arrays be fast than a vector (this would be overall performance of course). Any thoughts welcome.


Anon,



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why don''t you just make two passes through the data? It is the best way to tackle this situation.

Share this post


Link to post
Share on other sites
Use STL''s vector class. Using this class all memory management is done for you and all you have to do is add new elements to the end of the vector, You only need to make one pass thru the data too.

typedef struct
{
float x, y, z
} Vertex;

std::vector< Vertex > list;

// Read 3DS vertex data from file
Read3DSVertex( &vertex );

list.push_back( vertex );

---
Visit Particle Stream Studios - Home of Europa, Tachyon and winSkin

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi

I think I am just a stickler for speed, I hate doing two passes through data it just seems so wasteful and I am also starting to dislike linked lists (large ones at least). Vectors sounded so cool until Pseudo said that all the data in the array is copied over, I can think of many occasions were a task like that would be undesirable. I will do some performance tests on arrays, linked lists, vectors and linked lists of arrays for different situations.


Anon,

Share this post


Link to post
Share on other sites

  • Advertisement