|
-
Feb 5th, 2003, 05:23 AM
#1
Thread Starter
Hyperactive Member
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.
-
Feb 5th, 2003, 06:10 AM
#2
Hyperactive Member
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))
-
Feb 5th, 2003, 06:20 AM
#3
Thread Starter
Hyperactive Member
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.
-
Feb 5th, 2003, 07:02 AM
#4
Member
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
-
Feb 5th, 2003, 07:23 AM
#5
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
-
Feb 5th, 2003, 08:01 AM
#6
Frenzied Member
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.
-
Feb 6th, 2003, 12:41 AM
#7
Thread Starter
Hyperactive Member
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:
I should do:
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.
-
Feb 6th, 2003, 03:52 PM
#8
Hyperactive Member
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|