473,395 Members | 1,532 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,395 software developers and data experts.

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 1607
<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
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
by: Gene Wirchenko | last post by:
Is the following guaranteed safe? void InitInt(int & SomeInt) { SomeInt=3; return; } int main() {
13
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
by: geek.arnuld | last post by:
this is the programme: #include <iostream // std::cout int main() { int sum; std::cout << sum << "\n"; }
4
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
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
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
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.