Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How garbage values are assigned to variables in c

Tags:

c

memory

C code :

int a;
printf("\n\t %d",a); // It'll print some garbage value;

So how does these garbage values are assigned to uninitialized variables behind the curtains in C?

Does it mean C first allocates memory to variable 'a' and then what ever there is at that memory location becomes value of 'a'? or something else?

like image 530
VishalDevgire Avatar asked Mar 10 '13 19:03

VishalDevgire


People also ask

How values are assigned to a variable in C?

To assign a value to a C and C++ variable, you use an assignment expression. Assignment expressions assign a value to the left operand. The left operand must be a modifiable lvalue. An lvalue is an expression representing a data object that can be examined and altered.

Where do garbage values come from?

A garbage value occurs when you allocate a memory for your variable, but you don't assign a value to your variable. The memory of the variable may still have a value from the previous program.

Why am I getting garbage values in my array in C?

A garbage value— when you don't initiate your array and try to access its content. Segmentation fault— when you try to access something which is out of the scope for that array. Like when you try to access the 101th element of the array whose max size is 100.

What does it mean for a variable to be garbage?

While declaring a variable, the memory is allocated. But this variable is not assigned which means the variable a is not initialized. If this variable a is only declared but no longer used in the program is called garbage value.


3 Answers

Does it mean C first allocates memory to variable 'a' and then what ever there is at that memory location becomes value of 'a'?

Exactly!

Basically, C doesn't do anything you don't tell it to. That's both its strength and its weakness.

like image 93
Bart Friederichs Avatar answered Oct 28 '22 12:10

Bart Friederichs


Initially memory is having some values, those are unknown values, also called garbage values, when ever we declare a variable some memory was reserved for the variable according to datatype we specified while declaring, so the memory initial value is unknown value, if we initialize some other value then our value will be in that memory location.

like image 23
user3359484 Avatar answered Oct 28 '22 12:10

user3359484


Does it mean C first allocates memory to variable 'a' and then what ever there is at that memory location becomes value of 'a'? or something else?

Correct. It is worth mentioning that the "allocation" of automatic variables such as int a is virtually nonexistent, since those variables are stored on the stack or in a CPU register. For variables stored on the stack, "allocation" is performed when the function is called, and boils down to an instruction that moves the stack pointer by a fixed offset calculated at compile time (the combined storage of all local variables used by the function, rounded to proper alignment).

The initial value of variables assigned to CPU registers is the previous contents of the register. Because of this difference (register vs. memory) it sometimes happens that programs that worked correctly when compiled without optimization start breaking when compiled with optimization turned on. The uninitialized variables, previously pointing to the location that happened to be zero-initialized, now contain values from previous uses of the same register.

like image 24
user4815162342 Avatar answered Oct 28 '22 14:10

user4815162342