0

Possible Duplicate:
Function Free() in C doesn't work

I just asked a question about free(), check the link below. If I can't use if(pointer != NULL), then what can I use, just in case it doesn't work because of a stupid mistake I made.

Function free() in C isn't working for me

Community
  • 1
  • 1
madtapper
  • 231
  • 2
  • 6
  • 11
  • Some of the answers to your other question did explain exactly why there's no way to do this check. – David Z Oct 14 '11 at 23:23

2 Answers2

3

Whenever you free a pointer you should set it to null - then you can use your test safetly

free(pointer);
pointer=NULL;

if(!pointer)
{
    pointer=malloc(42);
}

You could even create your own free function so you dont forget to set to null.

void myfree(void **ptr)
{
    free(*ptr);
    *ptr=NULL;
}
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • 5
    That's not completely safe, and some would argue that it's a crutch. A contrived example: `p0 = malloc(42); p1 = p0; free(p0); p0 = NULL; /* p1 is now invalid */`. More realistic examples are possible, but this comment is too small to contain them. – Keith Thompson Oct 14 '11 at 23:35
  • 1
    Agree its impossible to protect another programmer from themselves when using C :-) – Adrian Cornish Oct 14 '11 at 23:40
  • 1
    I like to use the comma operator; `p=(free(p),NULL);` – Dave Oct 15 '11 at 00:20
  • 1
    `ptr = realloc(ptr,0);` realloc returns a new pointer, or if the size is 0, then it returns NULL. If it has to shrink the allocated memory size, it also calls free() on on the unnecessary area. – kazmer Nov 10 '16 at 13:59
0

Don't use naked pointers.

Use std::shared_ptr<OBJ> and std::unique_ptr<OBJ>

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • There are no namespaces or templates in C – Adrian Cornish Oct 14 '11 at 23:31
  • 6
    The question was marked as C++, I gave a C++ answer. When people ask questions tagged both C++ and C, usually they mean just C++. (Though I admit, the title should have tipped me off) – Mooing Duck Oct 14 '11 at 23:33
  • @MooingDuck: I think your logic is backwards. A lot of the times (here anyway) they are really C questions. – Jeff Mercado Oct 14 '11 at 23:39
  • 2
    @JeffMercado: I'd gotten the impression that they're new C++ coders who ask for a C answer because they know C answers also work, so they think the answers for the two languages will be the same. Matter of perception maybe. – Mooing Duck Oct 14 '11 at 23:42
  • @MooingDuck: I guess it's just that you haven't been here for too long. Give it some time, you'll see what I mean. ;) – Jeff Mercado Oct 15 '11 at 00:30
  • Looking at tags, Your C to C++ ratio is 8.7, while my C++ to C ratio is 12.8. I'd guess perception. – Mooing Duck Oct 15 '11 at 01:24
  • @Mooing Duck, I'm going to have to side with Jeff Mercado on this one. The questions that are tagged both C and C++ are usually C questions. They usually come from newbie programmers who have not yet understood that the two languages are very different. – Marlon Oct 15 '11 at 05:39