Have I lost memory on my hard disk due to memory leakage?

Until now I have always forgotten to free() the ptrs after use. I came to know now that this leads to memory leaks. Have I lost some bit of memory on my hard disk? How do I “free” it?

1 Like

Don’t worry. The memory will be freed after your program has finished its execution.

The memory that you did not free was allocated in RAM not hard disk. It is freed automatically. To free hard disk space (if any) run check disk utility.

Also don’t ask such questions here. Redirect such questions to appropriate forums.

Hello,

It seems you are confusing RAM memory with ROM memory and it also seems you are thinking on a memory leak as something catastrophic!!

First of all, critical system information like boot-records and possibly some controllers drivers are stored in what it’s known as ROM memory. ROM stands for Read-Only Memory, which is a non-volatile memory and it’s in a sense, persistent.

That’s why after you turn your computer off and on several times you can still access Firefox, Word, Excel, Media Player, and all other programs which are, in a sense, permanently stored on disk.

When you execute a program however, you are accessing what is known as RAM memory, which stands for Random Access Memory.

Unlike the previous memory, RAM memory is volatile, which means that it is wiped out after you turn off your computer or after you stop the execution of any process that is using it. That’s typically the reason why Hard Disks can go up to Terabytes in capacity (as they might be used to store many permanent information), while RAM memories are typically of the order of 2-4 Gigabytes (some thousands times smaller).

Hence, when you start a program, after you’ve compiled it, it is totally executed over RAM memory. On this sense, a memory leak is simply memory that contains garbage, or memory that was allocated but not actually used.

This typically arises when working with pointers maybe because a pointer in C IS a memory adress, that is, it serves as a variable which stores the adress of a specific RAM location, and it’s easy to mess things up and get memory leaks unless memory is well managed.

A good tool that it’s usually mentioned to see this is valgrind which checks for memory leaks under Linux.

Note however, that, after you stop executing your program and shut down your pc, all memory is cleared, so, you have nothing to worry about.

Also, thanks for the question, this is definitely NOT an easy subject!!

Best regards,

Bruno

2 Likes

The memory is allocated in main memory and not on hard disk. So you haven’t lost anything on hard disk. Also OS reclaims all the memory from process, including the leaked memory once a process terminates.

In small programs memory leakage is unlikely to cause trouble (goes un-noticed). But as a good programming practice always remember to free() the allocated memory. Or even better use C++ containers (no need to manage memory) or Smart pointers.