* I met a "strange" thing...
@ 2014-12-10 13:03 Graziano Servizi
2014-12-10 13:09 ` Jonathan Wakely
0 siblings, 1 reply; 3+ messages in thread
From: Graziano Servizi @ 2014-12-10 13:03 UTC (permalink / raw)
To: gcc-help
Hi,
by pure chance, during a copy+paste editing operation of a source code,
I ended up inadvertently with a code line like the following:
int * x = int();
Well, this code HAD BEEN COMPILED by g++ 4.8.3, and I cannot figure out
its meaning (from the compiler's point of view).
Here is a code sample of what happened to me:
int main()
{
int * x = int(); // compiled
*x = 888; // compiled
std::cout << *x << std::endl; // compiled, but segfault at execution
// I can understand the last event ...
}
What is the semantic of a kind of "default constructor of int" assigned
to a pointer?
Thanks for your attention.
G. Servizi
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: I met a "strange" thing...
2014-12-10 13:03 I met a "strange" thing Graziano Servizi
@ 2014-12-10 13:09 ` Jonathan Wakely
2014-12-10 13:22 ` Jonathan Wakely
0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2014-12-10 13:09 UTC (permalink / raw)
To: Graziano Servizi; +Cc: gcc-help
On 10 December 2014 at 13:03, Graziano Servizi wrote:
> Hi,
>
> by pure chance, during a copy+paste editing operation of a source code,
> I ended up inadvertently with a code line like the following:
>
> int * x = int();
>
> Well, this code HAD BEEN COMPILED by g++ 4.8.3, and I cannot figure out its
> meaning (from the compiler's point of view).
int() creates an integer with value zero.
In C++03 any integer expression with value zero is a valid null
pointer value, so you can do any of these:
int* p1 = 0;
int* p2 = '\0';
int* p3 = 1-1;
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: I met a "strange" thing...
2014-12-10 13:09 ` Jonathan Wakely
@ 2014-12-10 13:22 ` Jonathan Wakely
0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2014-12-10 13:22 UTC (permalink / raw)
To: Graziano Servizi; +Cc: gcc-help
On 10 December 2014 at 13:09, Jonathan Wakely wrote:
> On 10 December 2014 at 13:03, Graziano Servizi wrote:
>> Hi,
>>
>> by pure chance, during a copy+paste editing operation of a source code,
>> I ended up inadvertently with a code line like the following:
>>
>> int * x = int();
>>
>> Well, this code HAD BEEN COMPILED by g++ 4.8.3, and I cannot figure out its
>> meaning (from the compiler's point of view).
>
> int() creates an integer with value zero.
>
> In C++03 any integer expression with value zero is a valid null
> pointer value, so you can do any of these:
>
> int* p1 = 0;
> int* p2 = '\0';
> int* p3 = 1-1;
I should have said any integer *constant* expression.
The exact wording was:
"A null pointer constant is an integral constant expression (5.19)
prvalue of integer type that evaluates to zero or a prvalue of type
std::nullptr_t."
This was modified by
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#903 so
that only integer literals are valid, so now int() and 1-1 are not
valid null pointer constants.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-12-10 13:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-10 13:03 I met a "strange" thing Graziano Servizi
2014-12-10 13:09 ` Jonathan Wakely
2014-12-10 13:22 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).