uninitialised variable but NO error

Discussion in 'C++' started by geek.arnuld@gmail.com, Feb 4, 2007.

  1. Guest

    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/win32cpptut/html/w32cpptut_01_02_02.html
    , Feb 4, 2007
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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
    Mike Wahler, Feb 4, 2007
    #2
    1. Advertising

  3. Rolf Magnus Guest

    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).
    Rolf Magnus, Feb 4, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    7
    Views:
    542
    David Carlisle
    Jan 10, 2005
  2. Gene Wirchenko
    Replies:
    22
    Views:
    721
    Jeffrey Schwab
    Dec 19, 2003
  3. santosh

    Calling free() on an uninitialised pointer?

    santosh, Dec 4, 2005, in forum: C Programming
    Replies:
    13
    Views:
    424
    Keith Thompson
    Dec 5, 2005
  4. Replies:
    2
    Views:
    285
    John Ratliff
    Feb 4, 2007
  5. Len
    Replies:
    3
    Views:
    187
Loading...

Share This Page