Skip to main content
gamedev.net
🔒 Locked

array for BIG beginner

Started by Solomon Aug 6, 2002 at 2:46 AM 9 replies 1k views
Original Post
Solomon
Solomon
Hi, sorry for my English but can anybody tell me how to resize multidimensional array dynamicly? I have a class, where''s defined MyType Arr; and I want to resize it to a value loaded from a file. ex.: MyType Arr; ... somewhere later in code resize it to Arr[10][10](e.g.);] thanx Solomon
Pipo DeClown
Pipo DeClown
why cant you do this AFTER you loaded data from a file?
Solomon
Solomon
I need instance of this array to fill it with data from a file (from loaded dimensions i know, how many values will be loaded).
If you know another way how to do it, pleas, show me how(the best''ll be example). I''ll be glad.
blw
blw
Try dynamic array allocation using the "new" command. This topic appears in the forums several times, just go looking for it.

[edited by - blw on August 6, 2002 4:30:57 AM]
640 kByte of memory is all that any application should ever need(Bill Gates 1981)
nicho_tedja
nicho_tedja
quote:
Original post by blw
Try dynamic array allocation using the "new" command. This topic appears in the forums several times, just go looking for it.


The only thing I can't never understand is why people do not help those who need it. Searching for a specific topic in 30 pages of threads is a pain in the neck. If we just tell them to go find it, we basically create more troubles for them.

Anyway, for the person who asks this question, I'm not certain what kind of language you are talking about, but I assume you use C++.

To do this, you need a pointer. You can't use an instance of the class itself to make a dynamic array like that. So, instead of:

MyType Arr;

you write:

MyType* Arr=0;

However, this may result in a stricted dimensional array. For example, in the above example, you can only create 1 dimensional array. If you want to make two, do this:

MyType** Arr=0;

Some people get confused with this double pointers, so I suggest you to get used with pointers first before you try to do this.



My compiler generates one error message: "does not compile."

[edited by - nicho_tedja on August 6, 2002 4:56:55 AM]
My compiler generates one error message: "does not compile."
Pipo DeClown
Pipo DeClown
quote:
Original post by nicho_tedja
The only thing I can''t never understand is why people do not help those who need it. Searching for a specific topic in 30 pages of threads is a pain in the neck. If we just tell them to go find it, we basically create more troubles for them.



I totally agree with you!
da_flo
da_flo
I'll add things to nicho_tedja's reply...

Be aware that the memory allocation will be quite different than for a 1D array, since array[10][10] isn't really a 2D array, but rather an array of arrays.

So if you want to allocate arr[size1][size2], in C++ here you go :


    
MyType **Arr;
// some time later

Arr = new MyType *[size1]; // create an array of pointers of size "size1"

for (int i=0; i<size1; i++) Arr[i] = new MyType [size2];


Hope this helps

-----------------------
To keep the fresh style, fuck a duck and try to fly

[edited by - da_flo on August 6, 2002 5:36:41 AM]

[edited by - da_flo on August 6, 2002 5:38:46 AM]

[edited by - da_flo on August 6, 2002 5:39:38 AM]
-----------------------To keep the fresh style, fuck a duck and try to fly
Solomon
Solomon
Thanks to everybody.
Arrow Mk84
Arrow Mk84
Here''s some code I wrote for a string library I wrote. It uses malloc.h and should work for any data type. You have to use ''calloc'' to assign memory to the array before you use it and then ''free'' to give it back to the system when your done. The follow code calls ''realloc'' to simply add more memory to the end of the array (I''ve add comments so you can see what its doing). See MSDN or cplusplus.com for details on all three functions.


  
string_data = (char*)realloc(string_data, string_length + string2.string_length + 1); // add more memory to the array for the first string

strcat(string_data, string2.string_data); // append the second string to the first

string_length = strlen(string_data); // adjust the length tracker

Kylotan
Kylotan
Off-topic:
quote:
Original post by nicho_tedja
The only thing I can''t never understand is why people do not help those who need it. Searching for a specific topic in 30 pages of threads is a pain in the neck. If we just tell them to go find it, we basically create more troubles for them.

On the other hand, if people expect us to recite answers that they could have easily found for themselves, then they create troubles for us. I like helping, but it gets boring when people ask the same things, and it gets irritating when people ask something they could have easily found out for themselves. I don''t think this thread was like that, however - I am just making a general point.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
GroZZleR
GroZZleR
HEAVEN FORBID!

Oh dear God! Why would we EVER expect someone to click the "Search" button!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.