Board index » delphi » matrix (2-d array) operations: passing between functions, dynamic memory allocation

matrix (2-d array) operations: passing between functions, dynamic memory allocation

Hi,
I need help!!!!
I'm a novice C++ programmer, I've been teaching myself to program from scratch (though I have some experience
programming in LISP). I'm actually a biologist, and I'm trying to put together a statistical program that I can use to
calculate some statistics, and that I can distribute to make it easier for colleagues to calculate these statistics
also. My goal is to take a tab-delimited text file containing raw data (i*j rows and columns of numbers, either integers
or floating point decimal places), and read it into an array: int data[i][j] or float data[i][j]. I'd like to allocate this array
dynamically, so that the program can scan the file length and width and make an appropriately sized array.
I've been toying with this for a month now, and I can get dynamic allocation for a one-d array, but not 2-d. The help for (a 1998 version I think)
 Borland C++ includes some code for a dynamic 2-d array, but when I pasted it into the code editor and ran it, I got tons
 of error messages. I'd also like to be able to pass the 2-dimensional array as a parameter in function calls, and return the 2-d array
I'm stumped. Can anyone give me advice??

diboln...@ucdavis.edu

-Dan

 

Re:matrix (2-d array) operations: passing between functions, dynamic memory allocation


Hello.

Are you using the C++ new and delete operators?
If so then you would allocate your array like so:

int **AllocateArray(int width, int height)        // These variables must contain the dimensions of the desired array
{
    int **data;

    data = new int*[height];
    for(int y = 0; y < height; y++)
    {
        data[y] = new int[width];
    }

    return data;

Quote
}

To free up the memory latter you would go like this:

void DeleteArray(int **data, int height)
{
    for(int y = 0; y < height; y++)
    {
        delete data[y];
    }
    delete data;
    return;

Quote
}

This is just a simple example and it doesn't check for any allocation failures, but it should help.
To pass the 2-d array to another function you would just pass the variable data.  data will be a double pointer to an integer ( an int** ).  Inside
the function you will be able to index data just like in the calling function (data[2][3] = ... etc).

Ryan Sawatzky
rsawat...@openfox.com

Quote
Dan Bolnick wrote:
> Hi,
> I need help!!!!
> I'm a novice C++ programmer, I've been teaching myself to program from scratch (though I have some experience
> programming in LISP). I'm actually a biologist, and I'm trying to put together a statistical program that I can use to
> calculate some statistics, and that I can distribute to make it easier for colleagues to calculate these statistics
> also. My goal is to take a tab-delimited text file containing raw data (i*j rows and columns of numbers, either integers
> or floating point decimal places), and read it into an array: int data[i][j] or float data[i][j]. I'd like to allocate this array
> dynamically, so that the program can scan the file length and width and make an appropriately sized array.
> I've been toying with this for a month now, and I can get dynamic allocation for a one-d array, but not 2-d. The help for (a 1998 version I think)
>  Borland C++ includes some code for a dynamic 2-d array, but when I pasted it into the code editor and ran it, I got tons
>  of error messages. I'd also like to be able to pass the 2-dimensional array as a parameter in function calls, and return the 2-d array
> I'm stumped. Can anyone give me advice??

> diboln...@ucdavis.edu

> -Dan

Re:matrix (2-d array) operations: passing between functions, dynamic memory allocation


On 5 Jun 2001 12:35:21 -0700, "Dan Bolnick" <diboln...@ucdavis.edu> wrote:

Quote
>The help for (a 1998 version I think)  Borland C++

If it has that date, it must be from C++ Builder. Questions about Builder
should go in one of the .cppbuilder. groups. This group is for issues
re the pre-Builder BC++ compilers up to 5.02 (circa 1997).

Quote
>includes some code for a dynamic 2-d array,
>but when I pasted it into the code editor and ran it, I got tons
>of error messages.

Not much we can say if you don't tell us *which* example you used,
and post an *exact* copy of some of the error messages.

I know *one* of the examples of a 2d array (which uses calloc) had a
missing closing brace, and an incorrect typedef. As I recall, there's a
working example of a 2d array in the help for the new operator.

You might find the file ptrtutor.txt helpful, from http://www.snippets.org
As the site is under reconstruction, you may have to download the
entire collection in a zip file and extract what you want.

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:matrix (2-d array) operations: passing between functions, dynamic memory allocation


Quote
Ryan Sawatzky wrote:

> int **AllocateArray(int width, int height)        // These variables must contain the dimensions of the desired array
> {
>     int **data;

>     data = new int*[height];
>     for(int y = 0; y < height; y++)
>     {
>         data[y] = new int[width];

As you stated, you are in trouble if one of these new's throws.

Better use a container class.

Quote
>     }

>     return data;
> }

> void DeleteArray(int **data, int height)
> {
>     for(int y = 0; y < height; y++)
>     {
>         delete data[y];
>     }
>     delete data;

This has undefined behavior. delete [] data is correct.
Quote
>     return;
> }

Other Threads