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?

- 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

Reply via email to