That's the question, it came because you can't have default initialization values for the structs, so maybe that's the reason.
Printable View
That's the question, it came because you can't have default initialization values for the structs, so maybe that's the reason.
No. But if you use calloc to assign space, that should initialize it.
It depends on the scope in which a variable of struct type is declared.
ok i'll use memset.
Structure will be initialized in following circumstances
Please note: These where the results which i got on my machine.Code:---------------------------------
| ON | Initialized value |
---------------------------------
| Global var| 0 |
| Static | 0 |
| calloc | 0 |
| Local var | garbage |
| malloc | 0 |
---------------------------------
ssharish2005
You can't rely on calloc / memset producing valid initialisation for pointers and floats.
http://c-faq.com/malloc/calloc.html
> | Local var | garbage |
> | malloc | 0 |
Visual studio in debug mode specifically fills local variables and malloc'ed memory with known bit-patterns to assist with debugging all those 'use before initialisation' problems.
In general, you can't rely on either of them containing anything meaningful.
If you want a zeroised struct with 0, 0.0, NULL as appropriate, then do
Code:struct foo empty = { 0 };
// Then later, you can do
struct foo myvar = empty;
malloc isn't guaranteed to return initialized memory.
http://www.cplusplus.com/reference/c...ib/malloc.html
http://www.cplusplus.com/reference/c...ib/calloc.html