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 lives exactly as long as the function instance defining it — when that function intance returns, that memory’s free for recycling (if it’s housing a proper C++ object w/destructor and all, that dtor will be called). The heap lives until explicitly freed.
“How do you initialize a struct” (on either kind of memory!-) is a peculiar question — obviously via its automatically called ctor in C++ (if any), otheriwse with a
memcpyor the like — being in stack or heap makes no difference here.