473,480 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

uninitialised variable but NO error

this is the programme: [1]

#include <iostream // std::cout

int main()
{
int sum;
std::cout << sum << "\n";
}
when i compile it using gcc 4.1.2, i get this:

----------------------------------------------------------------------------
[unix@arch cpp]$ g++ first.cpp -o first
[unix@arch cpp]$ ./first
sum is: -1208774992
[unix@arch cpp]$
-----------------------------------------------------------------------------
i think the programme is wrong BUT why this does not give any error
like "uninitialised variable"?

[1] http://home.no.net/dubjai/win32cpptu..._01_02_02.html

Feb 4 '07 #1
2 1609
<ge*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
this is the programme: [1]

#include <iostream // std::cout

int main()
{
int sum;
std::cout << sum << "\n";
}
when i compile it using gcc 4.1.2, i get this:

----------------------------------------------------------------------------
[unix@arch cpp]$ g++ first.cpp -o first
[unix@arch cpp]$ ./first
sum is: -1208774992
[unix@arch cpp]$
-----------------------------------------------------------------------------
i think the programme is wrong BUT why this does not give any error
It does not because your code does not violate any language rules.
However, when you execute it, it will produce 'undefined
behavior' (even if it 'appears to work OK').
like "uninitialised variable"?
Some compilers can and do issue a 'warning' about this,
but there's no language requirement that one must.

Also, if your compiler can warn about this, it's possible
that you'll need to configure it to do so. Check your
documentation.

-Mike
Feb 4 '07 #2
ge*********@gmail.com wrote:
this is the programme: [1]

#include <iostream // std::cout

int main()
{
int sum;
std::cout << sum << "\n";
}
when i compile it using gcc 4.1.2, i get this:

----------------------------------------------------------------------------
[unix@arch cpp]$ g++ first.cpp -o first
[unix@arch cpp]$ ./first
sum is: -1208774992
[unix@arch cpp]$
-----------------------------------------------------------------------------
i think the programme is wrong BUT why this does not give any error
like "uninitialised variable"?
Because it doesn't violate the syntax rules of C++. It just invokes
undefined behavior. GCC can issue a warning in such a case, but only if you
enable optimizations (because only then it will do a more extensive code
flow analysis). To get a warning, add "-Wuninitialized -O" to the command
line options of g++.
As a minimum for the warning flags, you should add to the g++ command
line: "-ansi -pedantic -Wall -Wextra" (-Wall includes -Wuninitialized).

Feb 4 '07 #3

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

Similar topics

7
1791
by: Andy Fish | last post by:
consider the following fragment of xsl: <xsl:variable name="v1"><xsl:if test="false()"/></xsl:variable> <xsl:variable name="v2"></xsl:variable> <xsl:value-of select="boolean($v1)"/>...
22
1859
by: Gene Wirchenko | last post by:
Is the following guaranteed safe? void InitInt(int & SomeInt) { SomeInt=3; return; } int main() {
13
4964
by: santosh | last post by:
Hi, If I call free() with a uninitialised pointer, will the program state become undefined, or will free() return harmlessly? Incidentally, is there a way in Standard C to determine weather a...
2
277
by: geek.arnuld | last post by:
this is the programme: #include <iostream // std::cout int main() { int sum; std::cout << sum << "\n"; }
4
2587
by: BlueJ | last post by:
My program is to send data using socket.. but I got the error message: "sendto(msg) points to uninitialised byte(s) " This is my source code 1. key_list = emalloc(BUFSIZE - sizeof(Key) -...
19
1587
by: Ulrich Eckhardt | last post by:
Greetings! I was recently surprised by the compiler's warning concerning this code: struct text { char* s; size_t len; }; int main() { struct text t = {"hello world!"};
4
3728
by: Chronictank | last post by:
Hi, as a bit of background (and seeing as it is my first post :)) i am a complete newbie at perl. I literally picked it up a week ago to do this project as it seemed like the best choice for a...
0
7055
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
6920
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
7106
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
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5365
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,...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.