J
jimjim
Hello,
I am coming from a C background and the below dynamic allocation of an array
of pointers makes sense to me:
#define SIZE 2
int **p;
p = malloc ( SIZE * sizeof ( int * ));
for(int j=0; j<SIZE; j++)
p[j] = malloc( SIZE * sizeof ( int ) );
In C++, I have found out that the dynamic allocation of an array of pointers
looks like below:
int ** a ;
*a = new (int*)[SIZE];
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
<< endl << endl;
1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as in
C? The C++ compiler compains: "Cannot convert 'int *' to 'int * *' in
function main()"
2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
understand the last two zero addresses.
TIA
jimjim
I am coming from a C background and the below dynamic allocation of an array
of pointers makes sense to me:
#define SIZE 2
int **p;
p = malloc ( SIZE * sizeof ( int * ));
for(int j=0; j<SIZE; j++)
p[j] = malloc( SIZE * sizeof ( int ) );
In C++, I have found out that the dynamic allocation of an array of pointers
looks like below:
int ** a ;
*a = new (int*)[SIZE];
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
<< endl << endl;
1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as in
C? The C++ compiler compains: "Cannot convert 'int *' to 'int * *' in
function main()"
2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
understand the last two zero addresses.
TIA
jimjim