public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Use an undefined variable
@ 2008-03-06 18:09 pierozanetti
  2008-03-07 10:41 ` Andrew Haley
  0 siblings, 1 reply; 4+ messages in thread
From: pierozanetti @ 2008-03-06 18:09 UTC (permalink / raw)
  To: gcc-help


Hi, 
I have a question. This statement is accepted by the g++ compiler (v. 4.1.2,
under linux):

char c = 'a';
string s = s + c;

But the variable s is used before begin defined! Why? It's correct?

The program aborts when the statement is reached.
If we change the above code into:

string s = s + 'a';

the error changes. A segmentation fault is raised.
Can anyone tell me why the code is accepted?
Thanks

Piero Zanetti


-- 
View this message in context: http://www.nabble.com/Use-an-undefined-variable-tp15880213p15880213.html
Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Use an undefined variable
  2008-03-06 18:09 Use an undefined variable pierozanetti
@ 2008-03-07 10:41 ` Andrew Haley
  2008-03-07 11:44   ` Dario Saccavino
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Haley @ 2008-03-07 10:41 UTC (permalink / raw)
  To: pierozanetti; +Cc: gcc-help

pierozanetti wrote:
> Hi, 
> I have a question. This statement is accepted by the g++ compiler (v. 4.1.2,
> under linux):
> 
> char c = 'a';
> string s = s + c;
> 
> But the variable s is used before begin defined! Why? It's correct?

No, it is not correct.

> The program aborts when the statement is reached.
> If we change the above code into:
> 
> string s = s + 'a';
> 
> the error changes. A segmentation fault is raised.
> Can anyone tell me why the code is accepted?

There is an unbounded number of incorrect C++ programs.  We warn about
a subset of them.  Detecting every incorrect C++ program is, I'm sure
an uncomputable problem.

Andrew.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Use an undefined variable
  2008-03-07 10:41 ` Andrew Haley
@ 2008-03-07 11:44   ` Dario Saccavino
  2008-03-07 12:11     ` Andrew Haley
  0 siblings, 1 reply; 4+ messages in thread
From: Dario Saccavino @ 2008-03-07 11:44 UTC (permalink / raw)
  To: Andrew Haley; +Cc: pierozanetti, gcc-help

> There is an unbounded number of incorrect C++ programs.  We warn about
>  a subset of them.  Detecting every incorrect C++ program is, I'm sure
>  an uncomputable problem.
>

The -Wuninitialized and -Winit-self options are meant to check exactly
this kind of incorrect programs, and I'm quite disappointed that the
following function

template<class T> T f(T x)
{
    T y = x + y;
    return y;
}

generates a warning when instantiated with T = int, but not when T =
std::string.

Furthermore, my impression is that the use of an uninitialized integer
is "less incorrect" than the use of a complex object before even
calling its constructor. Several rules of the C++ language are meant
to guarantee that all objects are constructed (exactly once) before
their use, and destructed (exactly once) when they go out of scope.

   Dario

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Use an undefined variable
  2008-03-07 11:44   ` Dario Saccavino
@ 2008-03-07 12:11     ` Andrew Haley
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Haley @ 2008-03-07 12:11 UTC (permalink / raw)
  To: Dario Saccavino; +Cc: pierozanetti, gcc-help

Dario Saccavino wrote:
>> There is an unbounded number of incorrect C++ programs.  We warn about
>>  a subset of them.  Detecting every incorrect C++ program is, I'm sure
>>  an uncomputable problem.
>>
> 
> The -Wuninitialized and -Winit-self options are meant to check exactly
> this kind of incorrect programs, and I'm quite disappointed that the
> following function
> 
> template<class T> T f(T x)
> {
>     T y = x + y;
>     return y;
> }
> 
> generates a warning when instantiated with T = int, but not when T =
> std::string.

Interesting.

> Furthermore, my impression is that the use of an uninitialized integer
> is "less incorrect" than the use of a complex object before even
> calling its constructor. 

I have no idea where you got the idea that one is "less incorrect" than the
other.  I'm pretty sure it wasn't from the C++ standard.

Andrew.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-03-07 12:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-06 18:09 Use an undefined variable pierozanetti
2008-03-07 10:41 ` Andrew Haley
2008-03-07 11:44   ` Dario Saccavino
2008-03-07 12:11     ` Andrew Haley

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