473,606 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ 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 "uninitiali sed variable"?

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

Feb 4 '07 #1
2 1620
<ge*********@gm ail.comwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.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 "uninitiali sed 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*********@gma il.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 "uninitiali sed 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
1802
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)"/> <xsl:value-of select="boolean($v2)"/> it returns true for v1 but false for v2. (I'm using microsoft .net 1.1) how so? presumably v1 is a non-empty node set but I don't see how it could
22
1879
by: Gene Wirchenko | last post by:
Is the following guaranteed safe? void InitInt(int & SomeInt) { SomeInt=3; return; } int main() {
13
4989
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 pointer points to a valid block of allocated memory? Thanks for your time.
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
2597
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) - sizeof(byte) - sizeof(int)); bzero(key_list, sizeof(*key_list)); bp = key_list; memmove(bp, key, ID_LEN);
19
1602
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
3735
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 language to connect to a SQL database in a Unix envirment as well as script parsing so there is more than likely a ton of things i could have done a better way. I have been at this for 3 days now and cant for the life of me work out why the code is...
0
8015
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7951
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8094
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8305
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2448
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 we have to send another system
1
1553
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1296
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.