473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Use an undefinied variable

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 being 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 by the compiler?
Thanks

Piero Zanetti
Mar 7 '08 #1
10 1424
On Fri, 07 Mar 2008 01:16:56 -0800, 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 being defined! Why? It's correct?
It's valid code, but invokes undefined behaviour (actually I was a little
surprised that g++ doesn't emit a warning even with -std=c++98 -pedantic -
Wall).
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.
Still undefined behaviour.
Can anyone tell me
why the code is accepted by the compiler?
Because it's valid code.

--
Lionel B
Mar 7 '08 #2
On Fri, 07 Mar 2008 02:35:38 -0800, pierozanetti wrote:
On 7 Mar, 11:14, Lionel B <m...@privacy.netwrote:
>On Fri, 07 Mar 2008 01:16:56 -0800, 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 being defined! Why? It's correct?

It's valid code, but invokes undefined behaviour (actually I was a
little surprised that g++ doesn't emit a warning even with -std=c++98
-pedantic - Wall).
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.

Still undefined behaviour.
Can anyone tell me
why the code is accepted by the compiler?

Because it's valid code.

Use an undefined variable is a valid code?!?
Yes. To make it clear:

"valid" == complies with the C++ standard
"valid" != does something sensible

For instance this is valid code:

int main()
{
int x = 0/0;
}

Sure, it's extremely silly code (and my compiler issues a couple of
warnings) but it compiles.

--
Lionel B
Mar 7 '08 #3
pierozanetti wrote:
Use an undefined variable is a valid code?!?

He means "well formed." That is, the compiler is not required
to detect and issue a diagnositc for it.
Mar 7 '08 #4
Lionel B wrote:
On Fri, 07 Mar 2008 02:35:38 -0800, pierozanetti wrote:
[snip]
>Use an undefined variable is a valid code?!?

Yes. To make it clear:

"valid" == complies with the C++ standard
"valid" != does something sensible

For instance this is valid code:

int main()
{
int x = 0/0;
}

Sure, it's extremely silly code (and my compiler issues a couple of
warnings) but it compiles.
I think your example is actually ill-formed according to [5/5]

If during the evaluation of an expression, the result is not
mathematically defined or not in the range of representable values for
its type, the behavior is undefined, unless such an expression is a
constant expression (5.19), in which case the program is ill-formed.

The 0/0 is a constant expression for which the result is not mathematically
defined; and so the program is ill-formed.
Best

Kai-Uwe Bux
Mar 7 '08 #5
On Fri, 07 Mar 2008 09:52:13 -0500, Kai-Uwe Bux wrote:
Lionel B wrote:
>On Fri, 07 Mar 2008 02:35:38 -0800, pierozanetti wrote:
[snip]
>>Use an undefined variable is a valid code?!?

Yes. To make it clear:

"valid" == complies with the C++ standard "valid" != does something
sensible

For instance this is valid code:

int main()
{
int x = 0/0;
}

Sure, it's extremely silly code (and my compiler issues a couple of
warnings) but it compiles.

I think your example is actually ill-formed according to [5/5]

If during the evaluation of an expression, the result is not
mathematically defined or not in the range of representable values for
its type, the behavior is undefined, unless such an expression is a
constant expression (5.19), in which case the program is ill-formed.

The 0/0 is a constant expression for which the result is not
mathematically defined; and so the program is ill-formed.
Interesting... in that case shouldn't g++ report an error then rather
than just emit a warning?

$ g++ -std=c++98 main.cpp
main.cpp: In function ‘int main()’:
main.cpp:13: warning: division by zero in ‘0 / 0’
$

--
Lionel B
Mar 7 '08 #6
Lionel B wrote:
>string s = s + c;

It's valid code, but invokes undefined behaviour (actually I was a little
surprised that g++ doesn't emit a warning even with -std=c++98 -pedantic -
Wall).
How about this:

struct A { int i; A(): i(7) { std::cout << i << "\n"; } }(A);

Seems to compile and run ok with gcc. Is it valid?
Mar 7 '08 #7
On Fri, 07 Mar 2008 18:02:33 +0200, Juha Nieminen wrote:
Lionel B wrote:
>>string s = s + c;

It's valid code, but invokes undefined behaviour (actually I was a
little surprised that g++ doesn't emit a warning even with -std=c++98
-pedantic - Wall).

How about this:

struct A { int i; A(): i(7) { std::cout << i << "\n"; } }(A);

Seems to compile and run ok with gcc. Is it valid?
Perhaps I should clarify that by "valid" I mean conforms to the language
standard; i.e. has no syntax errors, is well-formed, whatever you'd like
to call it. So if a program compiles under a compiler that conforms
perfectly to the standard (probably such a compiler doesn't exist) then
it is by definition valid. Whether it runs "ok" (whatever that means) has
no bearing on its validity.

Your example compiles ok for me, prints out "7" to std output and I can't
see any syntax errors.

What point were you trying to make?

--
Lionel B
Mar 7 '08 #8
On 2008-03-07 10:23:32 -0500, Lionel B <me@privacy.netsaid:
>
Interesting... in that case shouldn't g++ report an error then rather
than just emit a warning?
The standard requires "a diagnostic." It doesn't distinguish between a
"warning" and an "error".

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Mar 7 '08 #9
Lionel B wrote:
On Fri, 07 Mar 2008 09:52:13 -0500, Kai-Uwe Bux wrote:
The 0/0 is a constant expression for which the result is not
mathematically defined; and so the program is ill-formed.

Interesting... in that case shouldn't g++ report an error then rather
than just emit a warning?
The standard says nothing about errors or warnings. There are required
diagnostics. The actual output is strictly a QOI issue. A perfectly
comforming implementation could output: "this is a diagnostic" for
every case.

Brian
Mar 7 '08 #10
On 7 mar, 17:02, Juha Nieminen <nos...@thanks.invalidwrote:
Lionel B wrote:
string s = s + c;
It's valid code, but invokes undefined behaviour (actually I was a little
surprised that g++ doesn't emit a warning even with -std=c++98 -pedantic -
Wall).
How about this:
struct A { int i; A(): i(7) { std::cout << i << "\n"; } }(A);
Seems to compile and run ok with gcc. Is it valid?
Yes. What do you think is wrong with it? (In this case,
there's not even any6 undefined behavoir.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 7 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.