That's the question, it came because you can't have default initialization values for the structs, so maybe that's the reason.
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
ok i'll use memset.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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