Jump to content
  • Advertisement

Archived

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

slyterence

Help with dynamic arrays

This topic is 6331 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!