This question was recently asked to me in an interview for which i went confused!!
"How do you initialize a structure in the heap memory ?"
could anybody please tell me the correct answer for this?
btw:how exactly are stack and heap memory are different from each other?
And looking about the above question some might also ask me about how do you initialize a structure on a stack memory?
.
may be this is a basic question or might be a wrong question too, but i am just curious to know!
Could anybody please help?
The stack is used for the allocation of local variables, the heap is used when you dynamically allocate memory, like with malloc(). In either case you will need to make sure you have initialize your structure. You can use something like calloc() to allocate your memory from the heap which automatically zeros it (malloc does not). And the variables on the stack are not initialized as well (if memory serves).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With