Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can g++ fill uninitialized POD variables with known values?

Tags:

c++

g++

I know that Visual Studio under debugging options will fill memory with a known value. Does g++ (any version, but gcc 4.1.2 is most interesting) have any options that would fill an uninitialized local POD structure with recognizable values?

struct something{ int a; int b; };
void foo() {
    something uninitialized;
    bar(uninitialized.b);
}

I expect uninitialized.b to be unpredictable randomness; clearly a bug and easily found if optimization and warnings are turned on. But compiled with -g only, no warning. A colleague had a case where code similar to this worked because it coincidentally had a valid value; when the compiler upgraded, it started failing. He thought it was because the new compiler was inserting known values into the structure (much the way that VS fills 0xCC). In my own experience, it was just different random values that didn't happen to be valid.

But now I'm curious -- is there any setting of g++ that would make it fill memory that the standard would otherwise say should be uninitialized?

like image 206
Bob Lied Avatar asked Apr 16 '10 14:04

Bob Lied


People also ask

What happens if we use an uninitialized variable where it is declared but no initial value is given?

An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.

What happens if you don't initialize a variable?

Initializing a variable means specifying an initial value to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.

What are the values assigned to uninitialized variables?

An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory location that the variable is using. This can lead to errors that are very hard to detect since the variable's value is effectively random, different values cause different errors or none at all.

What will be filled in the memory space if a variable is declared without initial value?

If we do not initialize the variable, then it will take in the garbage value. In Primary Type, we use built-in data types such as int, float, char, boolean, double, long etc. and in User Defined Type, we use user-defined data types such as struct, Union, enum, typedef etc.


1 Answers

Any C++ comiler can initialize any POD type to its "zero" value using the syntax:

int i = int();
float f = float();
MyStruct mys = MyStruct();
// and generally:
T t = T();

If you want to talk about debugging that's something else...

(By the way, I think VS had all uninitialized memory initialized to 0xCC when in "debug mode, so that no matter where you jump (e.g. call a bad function pointer) that's doesn't happen to be actual program code/data int3 is raised.)

like image 98
conio Avatar answered Nov 15 '22 19:11

conio