Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7981123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:16:47+00:00 2026-06-04T10:16:47+00:00

This program int main() { int a, b; a = b; return 0; }

  • 0

This program

int main()
{
    int a, b;
    a = b;
    return 0;
}

compiles without warnings or errors if you compile it with

g++ -Wall test.cpp

However, if you compile it with optimisations on, even at the same warning level

g++ -Wall -Os test.cpp

Then it will start complaining that you’re using uninitialised variables!
(Note: the actual program is slightly longer and doesn’t expose the problem as clearly as this little example here.)

I’ve tried to search with google, but either I’m not using the right search terms, or this is not a known phenomenon. Anyway, my questions basically are

  • Why would the compiler give this warning with -Os but not with -O0, even at the same warning level? (If anything, I’d expect it to be the other way around for this little example, since the variables can be optimised away entirely, and then the problem would vanish.)
  • Are there any other compiler options that trigger unexpected warnings like this? Since I’m interested in making my programs bug-free, I like to see all of them!
  • Is this a known feature? If so, where can I find it? I did try searching.
  • Or is this just a glitch in my particular version of the compiler (gcc 4.3.2-1.1, Linux, 32 bit)?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:16:48+00:00Added an answer on June 4, 2026 at 10:16 am

    The compiler needs to do extra checks in order to perform some optimizations, which leads it to emit extra warnings. There is a brief explanation of it in the relevant chapter of An Introduction to GCC.

    By the way, on my platform (32 bit ubuntu 12.04 on x86) the code produces warnings with gcc 4.6.3, gcc 4.7.0 and with a gcc 4.8 snapshot:

    uninitialized.cpp: In function ‘int main()’:
    uninitialized.cpp:3:9: warning: variable ‘a’ set but not used [-Wunused-but-set-variable]
    uninitialized.cpp:4:10: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this program int main() { float f = 11.22; double d = 44.55;
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
What should be the output of this C program? #include<stdio.h> int main(){ int x,y,z;
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
int main() { const int maxint=100;//The program will crash if this line is put
In trying to compile this program: namespace MyNamespace { template<typename T> class Test {
I am trying to compile the following program: #include <iostream> int main(){ std::cout <<
int main() { int i=3; (++i)++; printf(%d,i); } This programs works with g++ compiler
I have this C++ program: int box[9][9]; box = { {solution[0][0],solution[0][1],solution[0][2],solution[1][0],solution[1][1],solution[1][2],solution[2][0],solution[2][1],solution[2][2]}, {solution[0][3],solution[0][4],solution[0][5],solution[1][3],solution[1][4],solution[1][5],solution[2][3],solution[2][4],solution[2][5]}, {solution[0][6],solution[0][7],solution[0][8],solution[1][6],solution[1][7],solution[1][8],solution[2][6],solution[2][7],solution[2][8]}, {solution[3][0],solution[3][1],solution[3][2],solution[4][0],solution[4][1],solution[4][2],solution[5][0],solution[5][1],solution[5][2]},
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.