Why is this working?

J

jeffmax

Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];
p = (char(*)[]) new (char[3]);
(*p)[0] = 'c';
cout << (*p) << endl;
}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).
 
V

Victor Bazarov

Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];

OK, so 'p' is a pointer to a single-dimensioned array of chars.
p = (char(*)[]) new (char[3]);

So, you're allocating an array of three chars and casting the
pointer to its first element to a pointer to an array of chars.
I am not sure this should work without undefined behaviour.
(*p)[0] = 'c';

You're dereferencing the pointer to the array (and get the actual
array, or a reference to it), then you index within it. You're
getting a reference to the first element of that array. Then you
assign 'c' to it. Should be no problem.
cout << (*p) << endl;

You dereference the pointer to the array, get the array, it is
converted to a pointer to 'char', and printed. Since the rest of
the array is not initialised, it can cause undefined behaviour.
If the second or the third elements of the array just happen to
be zero, you get lucky.
}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).

It is. But (*p) is an array that decays to a pointer if you try to
apply indexing to it.

V
 
J

jeffmax

Thank you. I was not familiar with the concept of an array decaying to
a pointer.
Victor said:
Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];

OK, so 'p' is a pointer to a single-dimensioned array of chars.
p = (char(*)[]) new (char[3]);

So, you're allocating an array of three chars and casting the
pointer to its first element to a pointer to an array of chars.
I am not sure this should work without undefined behaviour.
(*p)[0] = 'c';

You're dereferencing the pointer to the array (and get the actual
array, or a reference to it), then you index within it. You're
getting a reference to the first element of that array. Then you
assign 'c' to it. Should be no problem.
cout << (*p) << endl;

You dereference the pointer to the array, get the array, it is
converted to a pointer to 'char', and printed. Since the rest of
the array is not initialised, it can cause undefined behaviour.
If the second or the third elements of the array just happen to
be zero, you get lucky.
}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).

It is. But (*p) is an array that decays to a pointer if you try to
apply indexing to it.

V
 
J

Joe Van Dyk

Thank you. I was not familiar with the concept of an array decaying to
a pointer.

I'm sure V would say "You're welcome", but he won't (check his signature).

Joe
Victor said:
Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];

OK, so 'p' is a pointer to a single-dimensioned array of chars.

p = (char(*)[]) new (char[3]);

So, you're allocating an array of three chars and casting the
pointer to its first element to a pointer to an array of chars.
I am not sure this should work without undefined behaviour.

(*p)[0] = 'c';

You're dereferencing the pointer to the array (and get the actual
array, or a reference to it), then you index within it. You're
getting a reference to the first element of that array. Then you
assign 'c' to it. Should be no problem.

cout << (*p) << endl;

You dereference the pointer to the array, get the array, it is
converted to a pointer to 'char', and printed. Since the rest of
the array is not initialised, it can cause undefined behaviour.
If the second or the third elements of the array just happen to
be zero, you get lucky.

}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).

It is. But (*p) is an array that decays to a pointer if you try to
apply indexing to it.

V
 

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,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top