Jump to content
  • Advertisement
Sign in to follow this  
serious_learner07

Problems in 2D dynamic array allocation.

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = ( 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[j] = 1.0;  //Crashing here.
}
What is wrong with the code.

Share this post


Link to post
Share on other sites
Advertisement
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 = ( 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[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 = 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 = 1.0;


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!