Memory management question

Hi all,

I've coded a bit in C++ these days and always wondered about memory management. So far, I've never consciously paid attention to memory management, and everything seems to work.
So my question is: when do I have to care about memory management? Do I have to delete strings, ints? Do I have to delete objects from classes? What happens if I don't?

Thanks!
You don't have to delete objects allocated with automatic or static memory:
1
2
int i; // doesn't need to be deleted
static int j; // doesn't need to be deleted 
You should do that when you allocate objects on the free store:
1
2
3
4
int p1 = new int;
int p2 = new int[5];
delete p1;
delete[] p2;
Read also this: http://www.cplusplus.com/forum/general/13425/
And this tutorial, if you need: http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
Topic archived. No new replies allowed.