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"?



    --
    http://arnuld.blogspot.com/
    Linux registered user #439610
    http://counter.li.org/

    [1] http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_02.html
     
    , Feb 4, 2007
    #1
    1. Advertising

  2. Salt_Peter Guest

    On Feb 4, 2:01 am, "" <>
    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 its not the compiler's responsability.
    It should however generate a warning and you can have the compiler
    generate an error instead (with the appropriate options).

    >
    > --http://arnuld.blogspot.com/
    > Linux registered user #439610http://counter.li.org/
    >
    > [1]http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_02.html
     
    Salt_Peter, Feb 4, 2007
    #2
    1. Advertising

  3. John Ratliff 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:


    If you have more questions about gcc, it would be best to ask them in a
    gcc specific newsgroup. This newsgroup is for standard C++ language
    questions.

    g++ should warn you if you turn on warnings. g++ -W -Wall -O2 first.cpp
    gives me a possibly uninitialized warning for your program.

    --John Ratliff
     
    John Ratliff, 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:
    565
    David Carlisle
    Jan 10, 2005
  2. Gene Wirchenko
    Replies:
    22
    Views:
    763
    Jeffrey Schwab
    Dec 19, 2003
  3. santosh

    Calling free() on an uninitialised pointer?

    santosh, Dec 4, 2005, in forum: C Programming
    Replies:
    13
    Views:
    447
    Keith Thompson
    Dec 5, 2005
  4. Replies:
    2
    Views:
    288
    Rolf Magnus
    Feb 4, 2007
  5. Len
    Replies:
    3
    Views:
    200
Loading...

Share This Page