c++ - If you use new to allocate space, when must you free it? -


we created binary tree structures in class used new create tnodes. have write freetree function it.

i have general question. if use new allocate space, when must free it?

when don't need allocated space anymore.

struct data { ... }  ...  data* data{new data}; data->something(); dosomethingwithdataptr(data);  delete data; 

obviously example simple, deciding when delete allocated space subjective... make sure delete it. (consider using smart pointers instead of new , delete avoid mistakes).


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

image - ClassNotFoundException when add a prebuilt apk into system.img in android -