Results 1 to 8 of 8

Thread: Newbie Q: Strange results with uninitialized variables

  1. #1

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319

    Newbie Q: Strange results with uninitialized variables

    Ok, I know this may be no big deal to you vets out there but something strange happend when I left a variable uninitialized at first. Here's the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
         int x;
    
         cout << "x = " << x << endl;
         x = x + 1;
         cout << "x = " << x << endl;
         system("pause");
         return 0;
    }
    When I ran this program, x return a value of 65 the first time and then 66 (obviously) the second time. 65 is a number I assigned to x in a previous program! Values in variables actually stay in variables between programs?! Does this have anything to do with memory leaks? If so, it makes sense.

    I'm used to visual basic in that the variables are always set to zero or null when first declared. Thanks to anyone who can answer my question.

  2. #2
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    variables are filled with random(?) data, I think. It may be scraps and leftovers. That is why you should use something like:

    Code:
    #define zout(x) memset(&x, 0x0, sizeof(x))
    VS.NET 2003

    Need to email me?

  3. #3

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by made_of_asp
    Code:
    #define zout(x) memset(&x, 0x0, sizeof(x))
    What does this do?



    edit:// that=>this
    Last edited by Comreak; Feb 5th, 2003 at 06:24 AM.

  4. #4
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34
    What i think you may have done is that u finished another project and went to file>New to create a new one, if u did,that might be what the problem was. Trying closing your program and reopening it with a completely new peoject and do the same thing. In order to go File>New u must close the older workspace.

    Hope this helps
    Death is always smiling down on us, the only thing we can do is smile back

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by made_of_asp
    variables are filled with random(?) data, I think. It may be scraps and leftovers.
    Quite correct. Variables are assigned an address in memory + how ever much space the variable takes up.

    If you don't zero out a variable, what ever is present at that address will be interpreted by the program as the variable's value. That is why initializing variables is so important.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    An image file (.EXE) is faulted into memory. Which means it gets assigned virtual addresses. Also it means, some actual memory is mapped into the image's working set.

    The data segment (where variables live) has addresses for variables. If you reference a variable whatever was in memory from another process, or from something your process did 5 minutes ago is still there.

    While this may seem to be anoying, and will cause program errors, unitialized variables also wreak havoc with optimization.
    One of the the standard optimizations for C and C++ compilers is
    to init (set to 0) all of the data segment. You can compile your code so it behaves like BASIC. What compiler are you using? Someone here will probably know what switch to set to turn on optimization at a low level which will handle thw problem.

    But now you know that uninitialized variables are bad news.

  7. #7

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by crptcblade
    Quite correct. Variables are assigned an address in memory + how ever much space the variable takes up.

    If you don't zero out a variable, what ever is present at that address will be interpreted by the program as the variable's value. That is why initializing variables is so important.
    So instead of:

    Code:
    int x;
    I should do:

    Code:
    int x = 0;
    Right?

    Originally posted by jim mcnamara
    While this may seem to be anoying, and will cause program errors, unitialized variables also wreak havoc with optimization.
    One of the the standard optimizations for C and C++ compilers is
    to init (set to 0) all of the data segment. You can compile your code so it behaves like BASIC. What compiler are you using? Someone here will probably know what switch to set to turn on optimization at a low level which will handle thw problem.

    But now you know that uninitialized variables are bad news.
    I'm using GCC 3.2 (minGW) for my compiler and Dev-C++ as my IDE. So If I turn on optimization, It will automatically set all my uninitialized variables to zero?
    Last edited by Comreak; Feb 6th, 2003 at 12:45 AM.

  8. #8
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Yes,

    And this is for char arrays.

    Code:
    #define zout(x) memset(&x, 0x0, sizeof(x))
    
    int main()
    {
     char z[25];
     zout(z);
     return 0;
    }
    VS.NET 2003

    Need to email me?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width