Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of uninitialized local char?

If you have lets say a local int that is uninitialized, then its gets an undefined value but if you have a local char variable should that not have an undefined value as well? Of course 0 could be that undefined value, but i was wondering if char is any different, since all related info i find is about int and the program below just outputs 0 when the char variable is cast to an int. Im using GCC 4.7 with no flags.

int main()
{
char test1;
int test2;

std::cout<<test2; //garbage
std::cout<<std::endl;
std::cout<<(int)test1; //0
    return 0;
}
like image 585
Finding Nemo 2 is happening. Avatar asked Jan 16 '23 07:01

Finding Nemo 2 is happening.


1 Answers

Uninitialised means really uninitialised. Just because you consistently get a particular value on your machine at a particular time, doesn't mean that will always be the case all the time on all machines.

You can verify that nothing is initialising your variable by dumping the assembly code for your function and inspecting it.

like image 80
Greg Hewgill Avatar answered Jan 20 '23 11:01

Greg Hewgill