• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

Dynamic memory allocation

Hi,

This question is not very specific to C++. How can I dynamically allocate memory to a three dimensional or a two dimensional array ?

Allocation of memory is no problem bu, keeping track of the offsets into the array is seomethinhg which will have to be done programatically. Is my conjecture correct ?

Please reply.

Shayad.
0
shayad
Asked:
shayad
  • 3
  • 2
1 Solution
 
chensuCommented:
Duplicate question. Delete it to refund.
0
 
alexoCommented:
new int[x][y][z];
0
 
chensuCommented:
If x, y and z are not constants, you can't find a data type to match the return type of new int[x][y][z].
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
alexoCommented:
Hmmm...  Indeed.
Then use vector<> of vector<> ad infinitum and forget about dynamic allocations.
0
 
gugaCommented:
For 2 dimensional array

int** array2d;
int     n = 100;
int     m = 50;

array2d = new int*[n]; // allocating memory for array of pointers
for(int i = 0; i < n;i++)
    array2d[i] = new int[m];

for(int k = 0; k < n;k++)
     for(int j = 0; j < m;j++) array2d[k][j] = 0;

//free memory
for(i = 0; i < n;i++) delete[] array2d[i]; //free memory for each                                                           //pointer
delete[] array2d;    //delete array of pointers
0
 
chensuCommented:
This is a duplicate question. I have already answered another question.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now