Dynamic memory in C++ | Sololearn: Learn to code for FREE!

+1

Dynamic memory in C++

Does memory allocated for dynamic variables automatically free up after the end of my program? Is it dangerous not to free up the memory with delete operator?

7/3/2020 6:18:55 AM

Maksim

8 Answers

New Answer

+1

Yes, pointers are just an implicit type of variables. This means that they are allocated in the stack memory. When local variables go out of their scope, they are automatically deleted. But pointers point to an address allocated in the heap memory, which is controlled only by the users. When pointer goes out of scope, it is automatically deleted, thus you will not know the address of the memory in the heap you need to free up. This means that while your program runs, you can not use anymore that part of the memory because it is locked automatically by the OS when it was reserved using the "new" keyword and will be unlocked and available for use only after you free it with the help of the "delete" keyword. Just remember this as a golden rule for any programming language: "Dynamically allocated memory needs to be freed up manually before exiting the program!". 👆

+5

Dynamically allocated memory stays allocated until it is explicitly deallocated or until the program ends. Memory leaks happen when your program loses the address of some bit of dynamically allocated memory before giving it back to the operating system.  Not calling delete means that freedoesn't get called and the destructor doesn't get called. The implications of not calling free on the allocated memory is a memory leak -- your program no longer can use that memory.

+5

Максим Казадаев what you are saying is absolutely possible and is known as memory leak. It might result in crashing of your computer if done inside an infinite loop. But you need not to worry about your computer because as soon as you shut down your computer, it's RAM is erased, also erasing those leaked memory cells.

+2

Thank you very much guys. And one more question to completely understand this topic. If you declare a pointer to some memory (int* pointer = new int;) in this scope: If (true) { int* pointer = new int; } And then program get out the } and variable pointer deletes. Does it mean that we have memory leak, because we lost the pointer and can't free up this memory?

+2

And should I not to free up a memory at the end of program because memory will freed up automatically after the end? Or is it a bad practice?

+2

In case of Int* pointer = new int; The pointer will be freed as soon as it goes out of scope , but not the object! The keyword *new* basically means that you allocate some memory for the object on *free store* - and you're responsible for calling delete sometime in order to release that memory.

+2

The "return" statement is nothing more than a condition to terminate the execution of a function, return a value if it's defined, and return to the calling process. main() is also a function and it's mandatory in the program. So when its body is executed, the underlying functions call will mark the main() function as its parent caller. But the main() itself is part of the program, its the root method. The program itself is a child process called by the "explorer.exe" as the parent process (in a standard case). When main() finishes its execution (encounters the "return" statement), the program tries to return to its parent calling process "explorer.exe". Here the OS tries to clear the memory allocated to and used by the program. The "return 0;" statement just indicates to the parent calling process that the program finished and returned a value of zero. In the parent process, we can check the returned value and take action. This is not the case if the program was assigned automatically under "explorer.exe".

0

If program goes to return 0; in main() does it mean that all allocated memory automatically freed up?