C ++ program automatically frees memory on crash?

I read in Google C ++ coding standards that Google does not use an exception. If an exception is not used, how do you free memory when errors occur in your program?

For example, f () calls g (), and if there is an error in g (), I have to free all the memory allocated in g (), and then throw an exception from f (). When f () catches the exception, f () will free all memory allocated in f () and exit the program.

If the exception is not used, and if there is an error in g (), can I force exit exit(0) to end and will the C ++ program be smart enough to free all the allocated memory? I assume that C ++ supports the stack and heap, and as soon as the program exits, C ++ will automatically free the stack and heap?

+6
source share
2 answers

The operating system cleans up all used memory and file descriptors when the process terminates for any reason.

+8
source

I heard that some types of memory, for example in Windows, the global COM heap memory cannot be freed for you. However, most of the memory / pens are cleared because the OS must cope with the condition that your application crashes. Of course, you can guarantee this in the case of local process memory and most pens, such as file descriptors, etc. In general, you can assume that the OS will clean up after you when your application exits.

Also, never follow the Google style guide. This is not for C ++, it is for C ++ minus everything you need to take to make it C. It might work for Google (doubtful), but it definitely won't work for anyone else.

-1
source

All Articles