Sign in to follow this  
serious_learner07

Problems in 2D dynamic array allocation.

Recommended Posts

I am trying to create a 2D array dynamically and then assigning values to it.But it is crashing during assignment. Code is below

void allocate2D(float** array, int nrows, int ncols) {
     
     /*  allocate array of pointers  */
     array = ( float** )malloc( nrows*sizeof( float* ) );
     
     /*  allocate each row  */
     int i;
     for(i = 0; i<=nrows; i++) {
          array[i] = ( float* )malloc( ncols*sizeof( float ) );
     }
 
}

void main()
{ 
 float **array;
 allocate2D(array,10,10);
 for(int i = 0;i<10;i++)
   for(int j=0;j<10;j++)
       array[i][j] = 1.0;  //Crashing here.
}
What is wrong with the code.

Share this post


Link to post
Share on other sites
The array variable is passed by value, not reference or pointer, so you're modifying a totally different variable. Why not return the array instead?

Example:

int**allocate2D(int nrows, int ncols) {

/* allocate array of pointers */
int** array = ( int** )malloc( nrows*sizeof( int* ) );

/* allocate each row */
int i;
for(i = 0; i < nrows; i++) {
array[i] = ( int* )malloc( ncols*sizeof( int ) );
}

return array;
}

void main()
{
int **array = allocate2D(10,10);
for(int i = 0;i<10;i++)
for(int j=0;j<10;j++)
array[i][j] = 1; //Crashing here.
}


Also remember to free() your array appropriately.

Share this post


Link to post
Share on other sites
You assign your allocated memory to a copy of the array pointer; the calling function doesn't get updated. Either use a reference to the int ** (assuming C++) a pointer to a int ** (int ***) or return the memory allocated as a return value.

Share this post


Link to post
Share on other sites
Quote:
Original post by serious_learner07
I am trying to create a 2D array dynamically and then assigning values to it.

You are actually creating an array of pointers to arrays. That is not a 2D array.

Quote:
Original post by serious_learner07
array = ( float** )malloc( nrows*sizeof( float* ) );

In C, the cast ist not necessary. In C++, you are much better of using a vector<vector<float> >.

Quote:
Original post by serious_learner07
void main()

This is illegal in both C and C++. Use int main() in C++ or int main(void) in C instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Also remember to free() your array appropriately.

It's quite a pain to free the float arrays and the float* array. Why don't we allocate all arrays with a single call to malloc? Then we only need to call free once when we are done with the array.


#include <stdio.h>
#include <stdlib.h>

float **allocate2D(int nrows, int ncols)
{
float **array = malloc(nrows*(sizeof(float*) + ncols*sizeof(float)));
float *payload = (float*)(array + nrows);
int i;
for (i = 0; i < nrows; ++i, payload += ncols)
array[i] = payload;
return array;
}

int main(void)
{
float **test = allocate2D(16, 10);
int r, c;
for (r = 0; r < 16; ++r)
for (c = 0; c < 10; ++c)
test[r][c] = 1.0;
free(test);
return 0;
}



Or, since the array is now contiguous in memory, we can use a single, non-nestet loop:

float *p = *test;
int i;
for (i = 0; i < 160; ++i)
p[i] = 1.0;


[Edited by - DevFred on September 18, 2008 9:25:20 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this