|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
| | #1 (permalink) |
| Banned Join Date: Nov 2003
Posts: 1,219
| [C++] Multi-Dimensional Array I'm needing the syntax for creating a multi-dimensional (We'll just say 2D.) array on the fly. The size of both dimensions will not be known until run-time. I've tried string string *fileArray = new string[x][y]; but it won't compile like that. string **fileArray = new string[x][y]; doesn't work either =\ I can't find any quick syntax solutions, only ways around this limitation. Does anyone out there know a fix? |
| | |
| | #2 (permalink) |
| Registered User Join Date: Jun 2002
Posts: 223
| Well, I don't use multidimensional arrays much (does anyone?), but I'm pretty sure your problem is that there is no operator new for multidimensional arrays. To dynamically create one, you have to use one of those so-called "ways around the limitation," e.g. new an array of pointers, then new each pointer to another array, ad nauseum... int** ppMy2DArray = new int*[x]; for ( int i = 0; i < x; ++i ) ppMy2DArray[i] = new int[y]; ... for ( int i = 0; i < x; ++i ) delete[] ppMy2DArray[i]; delete[] ppMy2DArray; Alternatively you can new one big 1D array and emulate the behavior of a 2D array yourself. It's a little messier but it will be a much faster operation with larger arrays. int** ppMy2DArray = new int*[x]; int* pData = new int[x*y]; for ( int i = 0; i < x; ++i ) ppMy2DArray[i] = &pData[i*y]; ... delete[] pData; // or *ppMy2DArray (a.k.a. ppMy2DArray[0]) delete[] ppMy2DArray; Last edited by Morb : 09-12-2007 at 12:26 PM. Reason: added comment by pData delete |
| | |
| | #4 (permalink) |
| Reactor Zero Join Date: Dec 2002
Posts: 319
| Is it specified that you MUST use multidimensional arrays of strings, AKA a 2D array of strings like this: string string string string string string string string string If it isnt specified like that, I'd suggest using Vectors or some other STL class, because they take care of the dynamic sizing for you. If not, theres a work around, but it takes time. |
| | |
| | #5 (permalink) |
| Banned Join Date: Nov 2003
Posts: 1,219
| Well I could do it any way I wanted initially and during my design process I decided to use a 2D dynamic array not knowing its limitations. It gave me trouble so I just rewrote it (using much simpler logic, took me like 15 minutes). This is just for my own personal information, at this point. I found three possible solutions. Two were already posted (the actual creation of the dynamic array and the emulation of one). The second is a bit more complicated, using malloc. I only have the logic for it though, haven't tried to implement it. Thanks for the responses though, guys. |
| | |
| | #6 (permalink) |
| Registered User Join Date: Aug 2002 Location: Paris
Posts: 355
| You're using C++, don't use malloc, use new. ... and to answer the question: The Boost Multidimensional Array Library (Boost.MultiArray) If you don't know Boost, you should definitely take a look at it. It has some really great stuff, some of which is going to be included in the next C++ standard. |
| | |
| | #7 (permalink) |
| upper management material Join Date: Nov 2002 Location: Orlando, FL
Posts: 2,040
+8 Internets | Is there any reason you aren't using vectors or lists? vector::vector - C++ Reference So it will be: Code:
Last edited by Froofy-D : 09-17-2007 at 06:49 PM. |
| | |
| | #8 (permalink) |
| Registered User Join Date: Aug 2002 Location: Paris
Posts: 355
| Quoted for emphasis. I pointed out boost multiarrays because I did not know what was the exact intent, but it's true that in the vast majority STL containers are more than enough to handle anything. When coding in C++ you should almost completely forget about any kind of C-style arrays, there are much more powerful and safe tools in the standard library. |
| | |
| | #9 (permalink) |
| Crankier than the Kong Join Date: Nov 2005 Location: DigiPen
Posts: 946
| Morb pretty much covered it, which is that you have to suballocate and do a bit of extra legwork for cleanup. It sucks, but there really is no way to do it with the basic new operator, for whatever silly reason. Crazy standard. |
| | |
| | #10 (permalink) |
| the illest motherfucker in a cardigan sweater Join Date: Jan 2005 Location: The CT
Posts: 4,303
| shoot me, in a class right now learning about multi dimensional arrays containing structs/classes...
__________________ ![]() Brekk [We R Bessy] Zul'Jin Shadowpriest |
| | |
| | #11 (permalink) | |
| Banned Join Date: Nov 2003
Posts: 1,219
| Quote:
"MAN OH MAN, WILL I USE AN ARRAY OF STRUCTS OR A STRUCT OF ARRAYS!?" | |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |