Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct way to initialize a dynamic pointer to a multidimensional array? [duplicate]

I've been having bad luck with with dynamic pointers when I range them to 2 dimensions and higher. For example I want a pointer to a 2D array. I know that:

int A[3][4];
int (*P)[4] = A;

Is completely legit (even if I don't completely understand why). Taking into consideration that:

int *P = new int[4];

works, I imagined that:

int **P = new int[5][7];

Would also work, but it's not. This code states the error:

Error: A value of type "(*)[7]" cannot be used to initialize an entity of
       type "int **"

By seeing this the new part becomes a pointer to an array of 7 integers I made:

int (*P)[4] = new int[7][4];

And this does work but it's not what I want to accomplish. By doing it like that I'm limited to at least using a constant value for any subsequent dimension, but I want it to be fully defined at run time and therefore "dynamic".

How could I go and make this multidimensional pointer work??

like image 731
Jader J Rivera Avatar asked Aug 16 '13 12:08

Jader J Rivera


People also ask

Which is the correct way to declare a multidimensional array in C?

The basic form of declaring a two-dimensional array of size x, y: Syntax: data_type array_name[x][y];

Which of the following options would you use for dynamically allocating a two-dimensional array Mcq?

The 2-D array arr is dynamically allocated using malloc.

How to allocate a multi-idimensional array of pointers?

For a multi-idimensional array, you need to allocate an array of pointers, then fill that array with pointers to arrays, like this: Here is what it looks like: Avoid memory leakage and dangling pointers! Show activity on this post. int **P = new int* [7]; p [0] = new int [5]; p [1] = new int [5]; ... Show activity on this post.

How do I create a multi-idimensional array?

For a multi-idimensional array, you need to allocate an array of pointers, then fill that array with pointers to arrays, like this: Here is what it looks like:

How do you access a 2 dimensional array with a pointer?

Pointer to Multidimensional Arrays Pointers and two dimensional Arrays: In a two dimensional array, we can access each element by using two subscripts, where first subscript represents the row number and second subscript represents the column number. The elements of 2-D array can be accessed with the help of pointer notation also.

How do you initialize a 2 dimensional array in Java?

Initialization of 2D Array There are two ways to initialize a two Dimensional arrays during declaration. int disp = { {10, 11, 12, 13}, {14, 15, 16, 17} };


2 Answers

Let's start with some basic examples.

When you say int *P = new int[4];

  1. new int[4]; calls operator new function()
  2. allocates a memory for 4 integers.
  3. returns a reference to this memory.
  4. to bind this reference, you need to have same type of pointer as that of return reference so you do

    int *P = new int[4]; // As you created an array of integer
                         // you should assign it to a pointer-to-integer
    

For a multi-idimensional array, you need to allocate an array of pointers, then fill that array with pointers to arrays, like this:

int **p;
p = new int*[5]; // dynamic `array (size 5) of pointers to int`

for (int i = 0; i < 5; ++i) {
  p[i] = new int[10];
  // each i-th pointer is now pointing to dynamic array (size 10)
  // of actual int values
}

Here is what it looks like:

enter image description here

To free the memory

  1. For one dimensional array,

     // need to use the delete[] operator because we used the new[] operator
    delete[] p; //free memory pointed by p;`
    
  2. For 2d Array,

    // need to use the delete[] operator because we used the new[] operator
    for(int i = 0; i < 5; ++i){
        delete[] p[i];//deletes an inner array of integer;
    }
    
    delete[] p; //delete pointer holding array of pointers;
    

Avoid memory leakage and dangling pointers!

like image 131
Deepak Ingole Avatar answered Oct 17 '22 23:10

Deepak Ingole


You want something like:

int **P = new int*[7];
p[0] = new int[5];
p[1] = new int[5];
...
like image 23
Paul Evans Avatar answered Oct 17 '22 23:10

Paul Evans