Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to free memory for structure variable

Tags:

c++

typdef struct _structname  
{  
    int x;  
    string y;  
} structure_name;

structure_name variable;

Now I access variable.x and y. After using it how can i deallocate or free the memory used by variable?

Actually the memory is getting allocated when i am doing variable.y="sample string".So the = operator allocates memory which is causing issue. how can i resolve it now?

like image 948
SPB Avatar asked Mar 09 '11 08:03

SPB


1 Answers

You've allocated the structure on the stack. The memory it's using will be freed when it goes out of scope. If you want to have control over when the memory is freed theh you should investigate dynamic memory allocation.

like image 84
Paul Mitchell Avatar answered Oct 17 '22 08:10

Paul Mitchell