Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not properly deleted pointers fragment memory?

That's basically the question. If I have a pointer int *a = &someIntVar and I do not delete it over the course of the program, does it stay in memory after the program has terminated? Is this a case of data fragmentation?

EDIT:

My mistake for using a bad example. So int *a = new int[100]; never gets deleted, and even if it gets deleted, an answer says that the pointee is deleted, not the pointer. Yet pointers also have a length. So the question is, on a Windows or Linux OS, does it automatically clean up after pointers? (Assume C++)

like image 404
IAE Avatar asked Apr 11 '26 20:04

IAE


2 Answers

Since you have not newed this pointer, you are not leaking memory.

Were you to do int* a = new int; and not delete a, then you would leak a.

As a rule of thumb, the number of news should equal the number of deletes.

When someIntVar goes out of scope, the stack is unwound and its memory will be freed and a will be left dangling, of course.

like image 73
Johnsyweb Avatar answered Apr 13 '26 09:04

Johnsyweb


No, all memory allocated by the process is usually freed when the process terminates. But this may not be the case for stuff like file handles or graphics resources (i.e. in Win32 device context handles, bitmaps etc.).

like image 29
humbagumba Avatar answered Apr 13 '26 10:04

humbagumba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!