Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

structure on a heap memory

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?

like image 447
Vijay Avatar asked Dec 06 '22 04:12

Vijay


1 Answers

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).

like image 165
Francis Upton IV Avatar answered Dec 27 '22 17:12

Francis Upton IV