4
Contributors
3
Replies
20
Views
4 Years
Discussion Span
Last Post by Banfa
0

When you declare a variable, the compiler gets a "box" and labels it with the name of your variable.
The kind of box taken, depends on the type of your variable. The "box" will be bigger if the type is float, than in case of an int.
Let us say you have as declaration: int myInt = 42;
So the compiler takes a "box" for an int and labels it with myInt. Now the problem is(because this "box" is taken out of free memory, that the box already has a value in it. So, initialisation is very important. Hope this little story helps a bit.

0

Here is another example why initialization is important:

int *pSomeInt;
...
if (pSomeInt != NULL)
{
free(pSomeInt);
}

pSomeInt is not initialized to NULL, therefore it is highly likely the compiler will assign some junk address. Since the if-statement could fail, the program could crash.

Motto: Always initialize your pointers to NULL!

0

Actually the standard says that use of any automatic variable before it has been either initialised or assigned is undefined behaviour, which as we all know is bad.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.