On Tue, 2007-05-22 at 13:34 -0600, David B Darrough wrote:
> I tried that and it works for a single dimension but if you add a second
> dimension, you get an error: non-constant expression as an array bound. Here
> is what I tried and had fail:
> 
> int a = 5;
> int b = 6;
> int *array;
> 
> array = new int[a][b];
> 
> ...
> 
> It fails at the array = new int[a][b] line because it wants a constant. Any
> other thoughts?

The reason it's not working is because you're actually not asking C++
(or C for that matter) to create a two-dimensional array here.  Remember
that arrays in C are really just pointers.  The way that the [] works is
by de-referencing pointers (at least in this instance).  So what you
asked C for in your code above was an array of integer arrays
(convertible but not equivalent to integer pointers).  The type of the
array you asked for was really int[b], which isn't a valid type in C
(it's indeterminate).

The only way I know of to get dynamically-allocated multi-dimensional
arrays in C is to do allocate one dimension at a time.  This is, alas,
not an order N operation:

array=new (int *)[a];
for(i=0;i<a;i++)
        array[i]=new int[b];


then you can access each element using your traditional [][] notation.




> - Dave
> 
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
> Behalf Of Byron Clark
> Sent: Tuesday, May 22, 2007 1:19 PM
> To: BYU Unix Users Group
> Subject: Re: [uug] Dynamically allocated arrays
> 
> On 5/22/07, David B Darrough <[EMAIL PROTECTED]> wrote:
> > My workaround has always been to create a vector, pushing onto the back of
> > it all of the elements,  then creating a function that takes an x and a y
> > value and indexes into the vector like it is a two dimensional array.
> There
> > has got to be a better way.... I have read something about dynamic arrays
> > being created on the heap with the new command at runtime but I'm not sure
> > how to use them. Any thoughts?
> 
> Here's how to do the dynamic arrays:
> 
> int a = 5;
> int *array;
> 
> array = new int[a];
> // use the array
> array[0] = 4;
> delete [] array;
> 
> This link shows one way of doing a multidimensional array on the heap:
> http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16
> --------------------
> BYU Unix Users Group 
> http://uug.byu.edu/ 
> 
> The opinions expressed in this message are the responsibility of their
> author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
> ___________________________________________________________________
> List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
> 
> 
> --------------------
> BYU Unix Users Group 
> http://uug.byu.edu/ 
> 
> The opinions expressed in this message are the responsibility of their
> author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
> ___________________________________________________________________
> List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list
> 

--------------------
BYU Unix Users Group 
http://uug.byu.edu/ 

The opinions expressed in this message are the responsibility of their
author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to