Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   dynamic allocation of an array of pointers (http://www.velocityreviews.com/forums/t452819-dynamic-allocation-of-an-array-of-pointers.html)

jimjim 03-25-2006 01:42 PM

dynamic allocation of an array of pointers
 
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



Heinz Ozwirk 03-25-2006 03:26 PM

Re: dynamic allocation of an array of pointers
 
"jimjim" <netuser@blueyonder.co.uk> schrieb im Newsbeitrag news:0NbVf.41831$wl.2734@text.news.blueyonder.co.u k...
> 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

Kai-Uwe Bux 03-25-2006 03:32 PM

Re: dynamic allocation of an array of pointers
 
jimjim wrote:

> 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

Kai-Uwe Bux 03-25-2006 05:17 PM

Re: dynamic allocation of an array of pointers
 
Heinz Ozwirk wrote:

> "jimjim" <netuser@blueyonder.co.uk> schrieb im Newsbeitrag
> news:0NbVf.41831$wl.2734@text.news.blueyonder.co.u k...
>> 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

jimjim 03-26-2006 02:05 PM

Re: dynamic allocation of an array of pointers
 
>> 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 !



jimjim 03-26-2006 02:10 PM

Re: dynamic allocation of an array of pointers
 
>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 !



jimjim 03-26-2006 02:12 PM

Re: dynamic allocation of an array of pointers
 
>> 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?

>> 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?
>

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



Kai-Uwe Bux 03-26-2006 03:13 PM

Re: dynamic allocation of an array of pointers
 
jimjim wrote:

>>> 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

jimjim 03-26-2006 03:53 PM

Re: dynamic allocation of an array of pointers
 
> 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



Kai-Uwe Bux 03-26-2006 04:35 PM

Re: dynamic allocation of an array of pointers
 
jimjim wrote:

>> 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


All times are GMT. The time now is 03:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.