1

I've encountered strange, unbelievable problem.

I wrote a program in which structure is reallocated several times.

The pointer is initially NULL, and before allocation it is checked whether NULL or it is deleted.

But, I've made a mistake here. I wrote like this if (!pConfig) delete pConfig;, which means it is never deleted. I thought that the process will be terminated with exception, but in fact, it is never terminated with this and making memory leaks.

Pseudo code below:

Config *pConfig = NULL;

void func() {
    if (!pConfig) delete pConfig;   // <<<<    Old wrong Code
//  if (pConfig) delete pConfig;    // <<<<    Right Code

    pConfig = new Config;
}

I used to know that deleting NULL pointer makes a program to fault exit, for years, since I've learned in colledge.
But from this embarrassing fact, I've confused.

Anybody helps me to understand this as well? Thanks.

Maybe it will be duplicated with Deleting a null pointer

coding monster
  • 384
  • 4
  • 18
  • 3
    [Is it safe to delete a NULL pointer?](https://stackoverflow.com/questions/4190703/is-it-safe-to-delete-a-null-pointer). – rawrex Aug 17 '21 at 01:34
  • @rawrex yeah, i'm writing codes like that, set null after deletion always, but here I made a little, shameful fault. – coding monster Aug 17 '21 at 01:38
  • 2
    Free tidbit: "makes a program to fault exit" is usually overstating the case. There are cases where a crash is rather likely in practice, but the C++ standard *never* mandates a crash. Instead the standard mandates "undefined behavior", which gives compilers free reign to do whatever is convenient. It might be convenient to produce CPU instructions that cause the OS to step in and halt your program. Or it might be convenient to optimize your code in such a way that the bad situation is skipped, avoiding a crash. *This is a side note; `delete nullptr;` is NOT undefined behavior.* – JaMiT Aug 17 '21 at 01:57
  • 2
    "*deleting NULL pointer makes a program to fault exit,*" - that has never been true. Calling `delete` on a NULL pointer has always been safe. "*for years, since I've learned in colledge*" - then you were taught wrong. – Remy Lebeau Aug 17 '21 at 02:02
  • 1
    @codingmonster -- *I used to know that deleting NULL pointer makes a program to fault exit, for years* -- If you were taught correctly, you would have just had `delete pConfig;` without any `if` statements. Then your code would have been correct -- goes to show you the problems bad and/or improperly taught C++ can cause. – PaulMcKenzie Aug 17 '21 at 02:07
  • @RemyLebeau - It has never been true in standard C or C++. However, early releases of C (before K&R C) did not specify the result of `free(NULL)`, and some early C implementations would crash on `free(NULL)` [I've seen that documented for PalmOS and 3BSD, and I understand but haven't verified it was true on other systems] and that carried into some early implementations of C++ between the ARM and the first (ratified) C++ standard (since early implementations of operators `new` and `delete` used `malloc()`/`free()` under the covers). – Peter Aug 17 '21 at 02:39
  • @Peter *It has never been true in standard C or C++* --[From the C++ FAQ](https://isocpp.org/wiki/faq/freestore-mgmt#delete-handles-null) – PaulMcKenzie Aug 17 '21 at 09:06
  • @PaulMcKenzie - Read my previous comment again. I also noted it was never true in standard C or C++. But C was originally developed at least 15 years before the first C standard was ratified (in 1989), and early versions of C++ were designed at least 8 years before the first C++ standard was ratified (in 1998). – Peter Aug 17 '21 at 09:48
  • *It has never been true in standard C or C++* -- I did read your comment. C++ became standardized in 1998. Are you saying that there are standard C++ compilers that did not adhere to the standard? If so, then the compiler is broken. – PaulMcKenzie Aug 17 '21 at 09:53

1 Answers1

4

Yes, you got memory leaks here. Memory leaks are common problems, but they won't cause your program to be killed immediately, sometimes when the system memory is running out, the OS will kill other programs that are not leaking, they are victims.

Delete a NULL pointer is always safe, and it's duplicated. What you have learned is wrong. And you may also view the doc from cpp reference.

For modern c++, it's always recommended to use smart pointers, we don't delete pointers by hand anymore, most leaks are avoided.

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42