+ 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?

3rd Jul 2020, 6:18 AM
Maksim
Maksim - avatar
8 Respostas
+ 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!". 👆
3rd Jul 2020, 5:08 PM
Vasile Eftodii
Vasile Eftodii - avatar
+ 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.
3rd Jul 2020, 6:41 AM
123
+ 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.
3rd Jul 2020, 6:55 AM
Arsenic
Arsenic - avatar
+ 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?
3rd Jul 2020, 11:56 AM
Maksim
Maksim - avatar
+ 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?
3rd Jul 2020, 11:59 AM
Maksim
Maksim - avatar
+ 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.
3rd Jul 2020, 12:21 PM
Arsenic
Arsenic - avatar
+ 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".
3rd Jul 2020, 5:42 PM
Vasile Eftodii
Vasile Eftodii - avatar
0
If program goes to return 0; in main() does it mean that all allocated memory automatically freed up?
3rd Jul 2020, 5:11 PM
Maksim
Maksim - avatar