Jump to content
  • Advertisement

Archived

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

Fluid FX

Arrays

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

Ok so this might come as a real newb question, but that''s why this is the beginners forum right? i have been around in programming a lot, made the step over to c++ about a year ago from Visual Basic, which is ofcourse a bad language for 3d coding(cause: memory speed lacks) anyway in Visual Basic, you could predefine an array, dim array(0) as string and later on redefine it, or resize it, how you want to call it.. by making this action: redim preserve array(size you want it) i was wondering, is there anyway to do this in C++? this code(in vb) actually makes it happen that the original array is copied, the new array will be made(the larger one) and the old array content will be placed back, and more arrays are back to fill. i heard something about array=new STRING[num] (tho string is not a variable) but i did not got this working yet?? thanks for your time! Jan

Share this post


Link to post
Share on other sites
Advertisement
If you want the easy solution, use std::vector (do a search for "STL vector" on Google and you''ll find information). If you don''t want to use std::vector, I''ll explain how to do it manually.

Share this post


Link to post
Share on other sites
hm that was not what i was looking for i think,

what i look for is resizing an array without losing the data,
so that i don''t have to be limited to a starting point of the array..

if you get it

thank you!

jan

Share this post


Link to post
Share on other sites
Really, the clean way to do what you want is to uses std::vector - that works like arrays in VB. An array in C is more primitive.

Here you can resize it by calling realloc() but this may change its position (i.e. invalidate pointers).

In VB when you used the "preserve" keyword and increase the array size a new array is made and the content of the old one is copied to it. In C you can either do this manually or use std::vector to get it done for you.

Share this post


Link to post
Share on other sites
well so i searched, but cannot find a clear exlpaning of the functions and that kind of stuff :S

is there any reference you would prefer me to read?
where it is explained clearly?

thank you

Jan

Share this post


Link to post
Share on other sites
quote:
Original post by felonius
Really, the clean way to do what you want is to uses std::vector - that works like arrays in VB. An array in C is more primitive.

Here you can resize it by calling realloc() but this may change its position (i.e. invalidate pointers).



Realloc() is nasty - like felonius says, you may well move the position of the array thus invalidating pointers. Also, it only works on dynamically allocated arrays.

Ideally, you would either:

a) Make your arrays big enough to store everything they will need to store, right from the beginning.

b) Use std::vector.




Share this post


Link to post
Share on other sites
The best guide/reference to STL available on the net is the one by SGI:

http://www.sgi.com/tech/stl/

The very first example uses vector.

Share this post


Link to post
Share on other sites
Goto gametutorials.com there is a really nice tutorial that teaches you how to use the basics of std::vectors.

Jeff D

Share this post


Link to post
Share on other sites
thank you all
i will try to find the solutions !!


another question, so i won''t post a new topic...

i want to make a small file load loop

and i got everything i needed,
eccept to find out when i am at the end of the file..

is anyone at the knowledge to do this??

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!