C++ Question.
Hey all,
I have a class with a multiple array in it
int array[arryx][arrayy]
Now, I can read from a file the data for this array fine, so long as the data is arrayx by arrayy.
If I read the size of the data beforehand, is tehre anyway to change the size of the structure?
Thanks in advance for any help, Brendan.
Brendan ''Locke'' Hennessy"I heard life sucks, so that''s why I don''t have one"
If I get you right then you want to read in the size of
the data at runtime and then store it in memory.
So I think what you wanna do is dynamic memory allocation.
Look up new and delete for C++,
or if you wanna do it C style you could use malloc() and free().
There are also other functions, but these are typically used.
I hope that helped you little!
the data at runtime and then store it in memory.
So I think what you wanna do is dynamic memory allocation.
Look up new and delete for C++,
or if you wanna do it C style you could use malloc() and free().
There are also other functions, but these are typically used.
I hope that helped you little!
Worth mentioning that if you need your arrays to change size AFTER they have been declared, it may be worth using vectors or something similar from the C++ standard template library. Otherwise its down to some messing with realloc() which can be nasty...
If not, ignore me and listen to Beast Master ;¬)
If not, ignore me and listen to Beast Master ;¬)
[size="1"]
or, if the size of the array changes a lot, look into linked lists. now if the data is only a number or two numbers, linked lists arent worth it, but if its an array of pointers to some data structure, a linked list is the way to go
If you want to do multi-dimensional dynamic arrays, do this:
I don''t think there is any simpler way anyway.
Hope this helps...
---------------
I finally got it all together...
...and then forgot where I put it.
|
I don''t think there is any simpler way anyway.
Hope this helps...

---------------
I finally got it all together...
...and then forgot where I put it.
...and a more detailed version (including file streaming):
Hope this helps
EDIT: Added an example of how to use it.
---------------
I finally got it all together...
...and then forgot where I put it.
Edited by - AdmiralBinary on January 25, 2002 1:21:11 AM
|
Hope this helps

EDIT: Added an example of how to use it.
---------------
I finally got it all together...
...and then forgot where I put it.
Edited by - AdmiralBinary on January 25, 2002 1:21:11 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement