Use an undefined variable

Andrew Haley aph@redhat.com
Fri Mar 7 10:41:00 GMT 2008


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.



More information about the Gcc-help mailing list