• Advertisement
Sign in to follow this  

C++ Memory Management (arrays/pointers)

This topic is 4722 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

Hi, I'm fairly new to c++, though I've used C a but, and am wondering what the best way would be to go about managing large amounts of data. Like, I may need an integer array thats 3D, and is 200x200x200, or even a lot larger. Back in C you couldnt just define the array without mallocing it. Heres how I was allocating it in regular C, but in 2D:
int **imatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a int matrix with subscript range m[nr1..nrh][ncl..nch] */ 
{

   long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
   int **m;

   /* allocate pointers to rows */
   m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
   if (!m) fprintf(stderr, "allocation failure 1 in matrix()");
   m += NR_END;
   m -=nrl;


   /* allocate rows and set pointers to them */
   m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
   if (!m[nrl]) fprintf(stderr, "allocation failure 2 in matrix()");
   m[nrl] += NR_END;
   m[nrl] -= ncl;

   for(i=nrl+1;i<=nrh;i++) m=m[i-1]+ncol;

   /* return pointer to array of pointers to rows */
   return m;

}

Thats from Numerical Recipes in C For 3D in C, i was just using a 1D array that was representitive of 3D. But this new thing I cannot use that, I dont want to have to calculate the 3D array indices from the 1D location. Is there a 3D array class I can use that makes the best use of memory allocation?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
But this new thing I cannot use that, I dont want to have to calculate the 3D array indices from the 1D location.


It's often better than to have arrays of arrays.

Quote:
Is there a 3D array class I can use that makes the best use of memory allocation?


Blitz++ or boost::multi_array

Share this post


Link to post
Share on other sites
The thing is, im running simulations where memory isnt the issue (physical memory) but computation time. Its a physical simulation so having an extra 4 lines of code , each with multiple operations, thats in every iteration woudl slow it down more I think that having to access arrays of arrays of arrays. But maybe Im wrong.
The systems im running it on have 4 to 8gb ram, so theres no swap file.

Share this post


Link to post
Share on other sites
Quote:
Original post by healeyx76
The thing is, im running simulations where memory isnt the issue (physical memory) but computation time. Its a physical simulation so having an extra 4 lines of code , each with multiple operations, thats in every iteration woudl slow it down more I think that having to access arrays of arrays of arrays. But maybe Im wrong.
The systems im running it on have 4 to 8gb ram, so theres no swap file.


when creating large multi-dimensional arrays its actually a little more efficent than having a single dimensional one and manually calculating offsets etc (because the compiler can do a much better job).

There is a big misconception when it comes down to creating dynamic C-style arrays in C++, for example you can create/destroy a 3D array in one go the catch is only the first dimension can be variable size the other dimensions must be known at compile time e.g.:


typedef int (&array_2d_ref)[300][300]; //reference to a 2d array
typedef int (&array_1d_ref)[300]; //reference to a 1d array

int (*array_ptr)[300][300] = new int[300][300][300];

array_2d_ref a = array_ptr[0];
array_1d_ref b = array_ptr[0][0];
int& int_ref = array_ptr[0][0][0];

delete[] array_ptr;


so if you need the other dimensions to be variable size aswell then for each dimesnion you turn it into a pointer but then you add more work to create & destroy etc.

Also you should know when you use operators new memory is allocated & then initialized, all in all go with Fruny's suggestion.

Share this post


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

  • Advertisement