Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C++ Dynamically Allocated Multi-dimensional Array

C++ Dynamically Allocated Multi-dimensional Array
Thread Tools
fromthecloud
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status: Offline
Reply With Quote
Oct 23, 2003, 12:26 PM
 
I'm working on a program right now for a CS class that's a treasure map. We're supposed to read in a data file and dynamically create the array to store it.

I create my array with the following statement:

int **newGrid = new int[initialRows][initialCols];

where initialRows and initalCols are interger values of the size the array needs to be.

However, I get the following message from g++ on the compile:

g++ -Wall -c treasure.cpp
treasure.cpp: In constructor `TreasureMap::TreasureMap(int, int,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
treasure.cpp:19: cannot convert `int (*)[((initialCols - 1) + 1)]' to `int**'
in initialization
make: *** [treasure.o] Error 1



Can anyone tell me what is wrong? Do I have my syntax set up incorrectly? Am I just stupid?

Thanks.
( Last edited by fromthecloud; Oct 23, 2003 at 12:32 PM. )
chown -R us:us yourbase

Dissent is not un-American.
     
BLAZE_MkIV
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Oct 23, 2003, 04:20 PM
 
to make a 2dimensional array in c++ you need to make a one dimmensional arraythen loop through and add the second dimension

p = new int*[ n ];
for ( i < n )
p[ i ] = new int[ m ];
     
smitty825
Forum Regular
Join Date: Sep 2003
Location: San Diego
Status: Offline
Reply With Quote
Oct 23, 2003, 06:50 PM
 
I would personally make it:

int *newGrid = new int[initialRows * initialCols];

...it seems to be what you're trying to go for...
     
Detrius
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status: Offline
Reply With Quote
Oct 23, 2003, 07:15 PM
 
Originally posted by fromthecloud:
I'm working on a program right now for a CS class that's a treasure map. We're supposed to read in a data file and dynamically create the array to store it.

I create my array with the following statement:

int **newGrid = new int[initialRows][initialCols];

where initialRows and initalCols are interger values of the size the array needs to be.

However, I get the following message from g++ on the compile:

g++ -Wall -c treasure.cpp
treasure.cpp: In constructor `TreasureMap::TreasureMap(int, int,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
treasure.cpp:19: cannot convert `int (*)[((initialCols - 1) + 1)]' to `int**'
in initialization
make: *** [treasure.o] Error 1



Can anyone tell me what is wrong? Do I have my syntax set up incorrectly? Am I just stupid?

Thanks.
Even though the C++ standard says you can do this, I've never gotten it to work. I agree with the other posters.

WHSoft

I have a matrix class that I set up to do exactly what you are wanting to do. Don't copy the code, because you will be expelled from school for it, but if you are stumbling across odd bugs in your code, this class has been rather extensively tested. You can compare your method to mine to see if there's anything obvious.
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
BLAZE_MkIV
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Oct 23, 2003, 08:39 PM
 
yeah the probably faster way would be the one dimensional array and do the math to make it seem 1 dimensional though indexing on the second axis may not be the fastest.
     
Gul Banana
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Oct 24, 2003, 11:22 AM
 
edit: was wrong, AND somehow managed to double post
( Last edited by Gul Banana; Oct 25, 2003 at 12:40 PM. )
[vash:~] banana% killall killall
Terminated
     
Gul Banana
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Oct 24, 2003, 11:23 AM
 
Edit: was wrong
[vash:~] banana% killall killall
Terminated
     
cheerios
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 25, 2003, 11:37 AM
 
Bananna, man... that'll make one BIG array, not a multi-dimensional one. That said, this compiles w/o errors:
Code:
int main (int argc, const char * argv[]) { int rows = 4; int columns = 6; int **x = new int* [rows]; for(int i =0;i< rows;i++) { x[i] = new int[columns]; } return 0; }
hope that helps.
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
johnMG
Registered User
Join Date: Oct 2003
Status: Offline
Reply With Quote
Oct 26, 2003, 02:26 AM
 
cheerios wrote:
that'll make one BIG array, not a multi-dimensional one.

Hmm. Maybe a post was edited, but it looks like you may have really been replying to smitty825. If so, I think he meant for the OP to fake a multi-dimensional array like so:

Code:
#include <iostream> using std::cin; using std::cout; using std::endl; int main() { cout << "number of rows?: "; int rows; cin >> rows; cout << "number of columns?: "; int cols; cin >> cols; int * array = new int[ rows * cols ]; // Fill 'em up. For a given row: for ( int y = 0; y < rows; ++y ) { // Move your way across all the columns: for ( int x = 0; x < cols; ++x ) { array[ ( y * cols ) + x ] = x + y; } } // Print 'em out, right-side-up though. cout << "\n"; for ( int y = rows - 1; y != -1; --y ) { // Move your way across all the columns: for ( int x = 0; x < cols; ++x ) { cout << array[ ( y * cols ) + x ] << "\t"; } cout << "\n"; } cout << endl; return 0; }
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Oct 26, 2003, 09:49 AM
 
Originally posted by cheerios:
Bananna, man... that'll make one BIG array, not a multi-dimensional one.
What's the difference? The dimensions are just an easy way of referencing different elements within a chunk of memory.
     
jessejlt
Mac Enthusiast
Join Date: Feb 2003
Location: Portland, Oregon
Status: Offline
Reply With Quote
Oct 26, 2003, 02:29 PM
 
I did an assignment like this last term, accept ours was a 3D array. After long hours of research and struggling, I found out that the best way to go about this is to overload the [] operator, which is very simple and allows you to create a very elegant nD array.
I would give you more clue-ins on how to do the [] operator, but researching how to do this is what makes the assignment beneficial. I"ll give you a hint though, if your [] operator is > 5 lines of code, you're thinking about it at too low a level of abstraction. Think what what you want to do, instead of how.
jesse ;-)
     
cheerios
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 26, 2003, 06:34 PM
 
ah, excuse me, I didn't fully understand what he was sayin'. got that 2D array in my head, and ddn't wanna think about nuffin' else

and yes, Bannana had a solution somewhat similar to JohnMG's. And, on thinking, it makes sense, just isn't exactly what the origional guy was askin' about, and would take a TEENY BIT more effort to keep track of where the "rows" were, when you wanted to access it. :shrug: Oh, and w/ school, a lot of times, they say "do it my way" and you had better!
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 11:55 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,