🔒 Locked
array for BIG beginner
Advertisement
Original Post
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
why cant you do this AFTER you loaded data from a file?
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]
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!
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 :
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]
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]
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
Off-topic:
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 ]
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 ]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement