Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
Or, use your gamerDNA username: (more...)
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-12-2007, 09:41 AM   #1 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
[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?
tikkus is offline   Reply With Quote
Old 09-12-2007, 11:59 AM   #2 (permalink)
Morb
Registered User
 
Join Date: Jun 2002
Posts: 223
-1 Internets
Send a message via AIM to Morb
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
Morb is offline   Reply With Quote
Old 09-12-2007, 12:18 PM   #3 (permalink)
Vinen
A Cat is Fine Too
 
Vinen's Avatar
 
Join Date: Oct 2002
Location: Not in fucking Acton, MA anymore!
Posts: 2,998
mmmm, last time I used one of those was Sophmore year of college. Greatest memory leak I ever wrote (I got lazy and forgot to clean up parts :3)
__________________
Vinna, Fat Bear of Stormrage
My Armory
Vinen is offline   Reply With Quote
Old 09-12-2007, 05:47 PM   #4 (permalink)
x1hundredregrets
Reactor Zero
 
x1hundredregrets's Avatar
 
Join Date: Dec 2002
Posts: 319
-5 Internets
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.
x1hundredregrets is offline   Reply With Quote
Old 09-12-2007, 07:03 PM   #5 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
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.
tikkus is offline   Reply With Quote
Old 09-15-2007, 07:44 AM   #6 (permalink)
Throag
Registered User
 
Join Date: Aug 2002
Location: Paris
Posts: 355
-1 Internets
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.
Throag is offline   Reply With Quote
Old 09-15-2007, 10:21 PM   #7 (permalink)
Froofy-D
upper management material
 
Froofy-D's Avatar
 
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:
vector[vector[string]] // but pretend the [ are < since the BB code doesn't seem to let you post multiple <
C++ lists or vectors should be fine in 95% of cases. Unless you are just making your own containers for learning purposes.

Last edited by Froofy-D : 09-17-2007 at 06:49 PM.
Froofy-D is offline   Reply With Quote
Old 09-16-2007, 02:43 AM   #8 (permalink)
Throag
Registered User
 
Join Date: Aug 2002
Location: Paris
Posts: 355
-1 Internets
Quote:
Originally Posted by Froofy-D View Post
C++ lists or vectors should be fine in 95% of cases. Unless you are just making your own containers for learning purposes.
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.
Throag is offline   Reply With Quote
Old 09-16-2007, 03:13 AM   #9 (permalink)
Nehrak
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.
Nehrak is offline   Reply With Quote
Old 09-27-2007, 04:22 PM   #10 (permalink)
brekk
the illest motherfucker in a cardigan sweater
 
brekk's Avatar
 
Join Date: Jan 2005
Location: The CT
Posts: 4,303
-19 Internets
Send a message via AIM to brekk
shoot me, in a class right now learning about multi dimensional arrays containing structs/classes...
__________________

Brekk [We R Bessy] Zul'Jin
Shadowpriest
brekk is offline   Reply With Quote
Old 09-27-2007, 06:05 PM   #11 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Quote:
Originally Posted by brekk View Post
shoot me, in a class right now learning about multi dimensional arrays containing structs/classes...
Ah yeah, that was fun. Actually, when we did that stuff that was the only time I enjoyed doing the design for programs.

"MAN OH MAN, WILL I USE AN ARRAY OF STRUCTS OR A STRUCT OF ARRAYS!?"
tikkus is offline   Reply With Quote
Old 09-27-2007, 06:34 PM   #12 (permalink)
brekk
the illest motherfucker in a cardigan sweater
 
brekk's Avatar
 
Join Date: Jan 2005
Location: The CT
Posts: 4,303
-19 Internets
Send a message via AIM to brekk
Quote:
Originally Posted by tikkus View Post
"MAN OH MAN, WILL I USE AN ARRAY OF STRUCTS OR A STRUCT OF ARRAYS!?"
you just blew my mind
__________________

Brekk [We R Bessy] Zul'Jin
Shadowpriest
brekk is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 01:17 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6