Note that there are some explanatory texts on larger screens.

plurals
  1. POarray of questions on pointers, double pointers, malloc, realloc and new (to set the record straight)
    primarykey
    data
    text
    <p>I'm catching up on pointers. I wrote down a few lines of code to test the different ways I could dynamically allocate a 2d array (posted at the bottom)</p> <p>My questions are the following:</p> <ol> <li>Should I use malloc in c++, or new? If I use new, can I still use realloc?</li> <li>When should I use realloc? What are the implications of using it in terms of performance and bugs?</li> <li>Out of the examples bellow, which objectNew version should I use? If the answer depends on the application, what are does it depend on?</li> </ol> <p>Many thanks, Matt</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct myObject { int data; myObject(int i) { data = i; } myObject() { data = 0; } }; int main(){ int r = 7; int c = 6; printf("Objects using NEW===============================\n"); //notice the triple pointer being assigned a new double pointer array myObject*** objectNew = new myObject** [r]; for(int i=0;i&lt;r;i++) { //objectNew is a 1D array of double pointers, however if we assign another layer of pointers, it becomes a 2D array of pointers to myObject objectNew[i] = new myObject* [c]; for(int j=0;j&lt;c;j++){ objectNew[i][j] = new myObject(10*i+j); //notice that we dereference data (-&gt;) printf("objectNew[%2d][%2d]=%02d\n",i,j,objectNew[i][j]-&gt;data); } } delete objectNew; printf("Objects using NEW version 2===============================\n"); //notice the triple pointer being assigned a new double pointer array myObject** objectNew2 = new myObject* [r]; for(int i=0;i&lt;r;i++) { //objectNew is a 1D array of double pointers, however if we assign another layer of pointers, it becomes a 2D array of pointers to myObject objectNew2[i] = new myObject [c]; for(int j=0;j&lt;c;j++){ objectNew2[i][j] = myObject(10*i+j); //notice that we dereference data (-&gt;) printf("objectNew2[%2d][%2d]=%02d\n",i,j,objectNew2[i][j].data); } } delete objectNew2; printf("Objects using MALLOC===============================\n"); //notice the double pointer being allocated double pointers the size of pointers to myObject myObject** objectMalloc =(myObject**) malloc(sizeof(myObject*)*r); for(int i=0;i&lt;r;i++) { //now we are assigning array of pointers the size of myObject to each double pointer objectMalloc[i] = (myObject*) malloc(sizeof(myObject)*c); for(int j=0;j&lt;c;j++){ objectMalloc[i][j] = myObject(10*i+j); //notice that we access data without dereferencing (.) printf("objectMalloc[%2d][%2d]=%02d\n",i,j,objectMalloc[i][j].data); } } free((void*) objectMalloc); //same as Malloc printf("Objects using CALLOC===============================\n"); myObject** objectCalloc = (myObject**) calloc(r,sizeof(myObject*)); for(int i=0;i&lt;r;i++) { objectCalloc[i] = (myObject*) calloc(c,sizeof(myObject)); for(int j=0;j&lt;c;j++){ objectCalloc[i][j] = myObject(10*i+j); printf("objectCalloc[%2d][%2d]=%02d\n",i,j,objectCalloc[i][j].data); } } free((void*) objectCalloc); printf("Int using NEW===============================\n"); //int is not an object int** intNew = new int* [r]; for(int i=0;i&lt;r;i++) { intNew[i] = new int[c]; for(int j=0;j&lt;c;j++){ intNew[i][j] = 10*i+j; printf("intNew[%2d][%2d]=%02d\n",i,j,intNew[i][j]); } } delete intNew; printf("Int using malloc===============================\n"); int** intMalloc =(int**) malloc(sizeof(int*)*r); for(int i=0;i&lt;r;i++) { intMalloc[i] =(int*) malloc(sizeof(int)*c); for(int j=0;j&lt;c;j++){ intMalloc[i][j] = 10*i+j; printf("intMalloc[%2d][%2d]=%02d\n",i,j,intMalloc[i][j]); } } free((void*) intMalloc); getchar(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload