dynamic allocation of an array of pointers

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
 
H

Heinz Ozwirk

jimjim said:
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()"

Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also compile, at least it does with Comeau's online compiler, but several other compilers seem to be confused by those extra parentheses, which looks too similiar to a cast, I guess.
2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
understand the last two zero addresses.

What would you expect with your C background when you see

p = calloc(SIZE, sizeof(int*));
p[SIZE] = calloc(SITE, sizeof(int));

?

You asked for undefined behaviour and you got it.

Regards
Heinz
 
K

Kai-Uwe Bux

jimjim said:
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;

I think, this should be:

#include <iostream>

const unsigned SIZE = 50;

int main ( void ) {
int ** a ;
a = new int* [SIZE]; // note: there are no parentheses around int*
for(int j=0; j<SIZE; j++)
a[SIZE] = new int[SIZE];
std::cout << &a << " " << a << " "
<< &a[0] << " " << a[0] << " " << &a[0][0]
<< std::endl << std::endl;

}

1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as

Try: "a = new int* [SIZE];"

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.

What do you expect to see instead?



Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Heinz said:
jimjim said:
Hello, [snip]
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()"

Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also
compile, at least it does with Comeau's online compiler, but several other
compilers seem to be confused by those extra parentheses, which looks too
similiar to a cast, I guess.

Actually, "a = new (int*)[SIZE]" does *not* compile with Comeau:

"ComeauTest.c", line 7: error: a value of type "int *" cannot be assigned to
an
entity of type "int **"
a = new (int*) [SIZE];
^

[snip]


Best

Kai-Uwe Bux
 
J

jimjim

Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also
compile, at least it does with Comeau's online compiler, but several
other
compilers seem to be confused by those extra parentheses, which looks too
similiar to a cast, I guess.

Actually, "a = new (int*)[SIZE]" does *not* compile with Comeau:

"ComeauTest.c", line 7: error: a value of type "int *" cannot be assigned
to
an entity of type "int **"
a = new (int*) [SIZE];
^
a = new (int*)[SIZE] does compile with Borland_bcc_5.5 !
 
J

jimjim

Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also
compile, at least it does with Comeau's online
compiler, but several other compilers seem to be confused by those extra
parentheses, which looks too similiar to a
cast, I guess.

You reckon the parentheses makes it look like a cast? What does the "new
(int*) [SIZE]" statement do?
What would you expect with your C background when you see

p = calloc(SIZE, sizeof(int*));
p[SIZE] = calloc(SITE, sizeof(int));

You asked for undefined behaviour and you got it.

That was a typo! It should be p[j] indeed !
 
J

jimjim

1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as
Try: "a = new int* [SIZE];"
Thanx for pointing it out!

What does the "new (int*) [SIZE]" statement do?
What do you expect to see instead?
That was a typo! It should be p[j] indeed !
 
K

Kai-Uwe Bux

jimjim said:
1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];"
as

Try: "a = new int* [SIZE];"
Thanx for pointing it out!

What does the "new (int*) [SIZE]" statement do?

I think it does the following:

new (T)

and

new T

both allocate an object of type T and returns a T* to that object. However,

new T [SIZE]

and

new (T) [SIZE]

are parsed differently: the first does what you expect, the second parses as

( new (T) ) [SIZE]

in other words, an object of type T is created and a pointer T* is returned;
then that pointer is reinterpreted as an array and the entry at index SIZE
is returned. The whole expression, therefore, has type T. This explains why
you were able to write

*a = new (int*) [Size];

the right hand has type int* as does the left hand.


Best

Kai-Uwe Bux
 
J

jimjim

in other words, an object of type T is created and a pointer T* is
returned;
then that pointer is reinterpreted as an array and the entry at index SIZE
is returned. The whole expression, therefore, has type T. This explains
why
you were able to write

*a = new (int*) [Size];

the right hand has type int* as does the left hand.
Hi..thx for the reply!

*a is an int*, agreed.

You said that "The whole expression, therefore, has type T". I cannot
understand how you have reached to the conclusion that the left hand side is
a int*, tough.

TIA
 
K

Kai-Uwe Bux

jimjim said:
in other words, an object of type T is created and a pointer T* is
returned;
then that pointer is reinterpreted as an array and the entry at index
SIZE is returned. The whole expression, therefore, has type T. This
explains why
you were able to write

*a = new (int*) [Size];

the right hand has type int* as does the left hand.
Hi..thx for the reply!

*a is an int*, agreed.

You said that "The whole expression, therefore, has type T". I cannot
understand how you have reached to the conclusion that the left hand side
is a int*, tough.

Huh? The left hand is a* and a* is an int*.

If you meant the right hand side, let's just apply what was said:

new (T) [Size] has type T

thus, with T=int*

new (int*) [Size] has type int*


Best

Kai-Uwe Bux
 
J

Jakob Bieling

jimjim said:
Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should
also compile, at least it does with Comeau's online compiler, but
several other
compilers seem to be confused by those extra parentheses, which
looks too similiar to a cast, I guess.

Actually, "a = new (int*)[SIZE]" does *not* compile with Comeau:

"ComeauTest.c", line 7: error: a value of type "int *" cannot be
assigned to
an entity of type "int **"
a = new (int*) [SIZE];
^
a = new (int*)[SIZE] does compile with Borland_bcc_5.5 !

Expect Comeau to be right here. VS8 also rejects your code,
suggesting as well that Comeau is right.

regards
 
P

perstam

OK, I have a follow-up question

I have to pass a pointer as a parameter to a function by reference an
have to dynamically allocate an array of arrays that is referenced b
that parameter pointer

Here's my function header
void CreateStructure (short * & Ptr

How do I modify the syntax in the previous posts to this thread t
dynamically allocate the array of arrays, then what is the syntax fo
assigning an address to, say, the first element of that array o
pointers
 
F

Fei Liu

perstam said:
OK, I have a follow-up question.

I have to pass a pointer as a parameter to a function by reference and
have to dynamically allocate an array of arrays that is referenced by
that parameter pointer.

Here's my function header:
void CreateStructure (short * & Ptr)

How do I modify the syntax in the previous posts to this thread to
dynamically allocate the array of arrays, then what is the syntax for
assigning an address to, say, the first element of that array of
pointers?

Please quote what you are replying to so we can see the context and
references of your questions.
 
K

Kai-Uwe Bux

perstam said:
OK, I have a follow-up question.

I have to pass a pointer as a parameter to a function by reference and
have to dynamically allocate an array of arrays that is referenced by
that parameter pointer.

Here's my function header:
void CreateStructure (short * & Ptr)

How do I modify the syntax in the previous posts to this thread to
dynamically allocate the array of arrays, then what is the syntax for
assigning an address to, say, the first element of that array of
pointers?

You are better off not knowing the syntax. Redesign the whole thing to use a
vector of vectors instead of an array of arrays. That way, memory handling
is taken care of. In case you actually want a matrix class, there are some
suggestions in the FAQ about that.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top