wht is garbage value?? how it generates and why it generates?? why default value is not null in c/c++
What is garbage value in c/c++??
Collapse
X
-
Tags: None
-
A garbage value is a vaue that is not correct for your variable. Like a variable named Month with a value of 32 when there are only 12 months. -
Anchal,
weaknessforcats is an absolute C++ Guru, and a darned fine teacher, too; so read carefully everything she writes.
If you want a "garbage" value like "Empty" or "Nothing" as in VBA, there is no such thing in C++. There is always a value in a C++ variable. That value will be used in all subsequent calculations using that variable, until the variable is overwritten.
If you corrupt a variable (say by overrunning an array that's on the stack), then the corrupt value will be used quietly in subsequent calculations. The lesson being that your functions should check all arguments when called.
In general, NULL is to pointers, what "Nothing" is to objects in VBA, however there is no equivalent of "Empty" or the "Null" value for variants.
Cheers!
OralloyComment
-
The following discussion is for C. Your C++ mileage may vary ...
Static variables are automatically initialized to 0 unless you specify some other initializer value.
Automatic variables are not automatically initialized unless you have an explicit initializer. The value of a no-initializer automatic variable is unknown (that is, garbage) until an executable statement puts a value in it.
Heap variables do not support initializers. The value of a heap variable is unknown (that is, garbage) until an executable statement puts a value in it (unless you used calloc to allocate the heap variable).Comment
Comment