From the freestore

T

Tomás

The following works as expected:


unsigned* p_number = new unsigned;

*p_number = 5;

delete p_number;



However, the following won't compile:


unsigned (*p_numbers)[5] = new unsigned[5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_numbers;



My compiler gives me the following error:


cannot convert `unsigned int*' to `unsigned int (*)[5]'


"new" is returning an expression of type "int *" rather than "int (*)[5]".
My first question is, Why did they make it so? I don't see the rationale.

Secondly, does the Standard explicitly specify that "new" returns a pointer
to the first element?


Thirdly, how do you somehow turn it into a pointer to an array? Is the
following valid (I've made it verbose for clarity):


unsigned* p_first_number = new unsigned[5];

void* p_temp = p_first_number;

unsigned (*p_numbers)[5] = static_cast<unsigned (*)[5]>( p_temp );

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_first_number;


I've used a "void*" in the above rather than using "reinterpret_cast".

So is there a pefectly legal way to get a pointer/reference to an array (for
something allocated using "new")?


-Tomás
 
P

Puppet_Sock

Tomás wrote:
[snip]
However, the following won't compile:

unsigned (*p_numbers)[5] = new unsigned[5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_numbers;

My compiler gives me the following error:

cannot convert `unsigned int*' to `unsigned int (*)[5]'
[snippers]

Sure. What does int (*) suggest to you? Hint, look up
"pointer to function" and see what you find. Putting the
( ) round (*p_numbers) changes the type.

You need to read up on the syntax of what's going on here.
It looks like you've just guessed what it should be. Get
something like _The C++ Language_ and read the first
100 pages or so very carefully. Also, read through this
group and find the C++ FAQ and read it carefully.
Socks
 
R

Rolf Magnus

Tomás said:
The following works as expected:


unsigned* p_number = new unsigned;

*p_number = 5;

delete p_number;



However, the following won't compile:


unsigned (*p_numbers)[5] = new unsigned[5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

new[] returns a pointer to the first element of the array, not a pointer to
the array itself. So make that:

unsigned* p_numbers = new unsigned[5];
p_numbers[0] = 5;
p_numbers[1] = 6;
delete [] p_numbers;



My compiler gives me the following error:


cannot convert `unsigned int*' to `unsigned int (*)[5]'



"new" is returning an expression of type "int *" rather than "int (*)[5]".

No. new[] is. It's another beast.
My first question is, Why did they make it so? I don't see the rationale.

Because it's normal to use an array through a pointer to its first element,
and the syntax is simpler.
Secondly, does the Standard explicitly specify that "new" returns a
pointer to the first element?
Yes.

Thirdly, how do you somehow turn it into a pointer to an array?

Why would you want to?
Is the following valid (I've made it verbose for clarity):


unsigned* p_first_number = new unsigned[5];

void* p_temp = p_first_number;

unsigned (*p_numbers)[5] = static_cast<unsigned (*)[5]>( p_temp );

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_first_number;

I've used a "void*" in the above rather than using "reinterpret_cast".

So is there a pefectly legal way to get a pointer/reference to an array
(for something allocated using "new")?

If you really want a pointer to the array, you can allocate an array of
arrays (or rather of one array), like:

unsigned (*p_first_number)[5] = new unsigned[5][1];
 
T

Tomás

unsigned (*p_first_number)[5] = new unsigned[5][1];


Exactly what I was looking for! Although it should be: unsigned[1][5]

Now we can do this:

void Array()
{
unsigned (*p_numbers)[5] = new unsigned[1][5];

(*p_numbers)[0] = 5;

(*p_numbers)[1] = 6;

delete [] p_numbers;
}


-Tomás
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top