Using NULL in C

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

DWW

Platinum Member
Apr 4, 2003
2,030
0
0
Originally posted by: mattg1981
I just compiled it using DWW's method of

free(words[k].word);
words[k].word = NULL;

and that worked too ... so let me see if I have this right:

free(words[k].word) -> frees or unmalloces the memory that I owned .. (what exactly does this mean though? ... did I just kill the pointer to it or did I erase what was in that memory address or did I just tell the OS .. "HERE you go, I dont need it anymore")

and then the words[k].word = NULL ... I dont know what I need this really .. I just freed it in the line above ... could you explain this one please :)

As long as you use "free" for each piece that you use "malloc" for, then there will be no memory leaks. Remember that. For every "malloc" there should be a corresponding "free" no matter what.

YES. free(words[k].word) frees the memory that you allocated (don't say unmallocates its not a word ;) You did not kill the pointer to it. The pointer still EXISTS to it (that is why the line afterward makes the pointer point to NULL... NULL is a universal expression meaning non-existant in C).

For example, RIGHT after you free it...the pointer would still point to that piece of memory but you would no longer own it (the environment takes care of giving it back to the system). but if you tried to access that piece of memory through your pointer it would be a WRONG thing. You don't own it and you don't know what it points to anymore...something else is probably using it by that point in time. That is why you must set the old pointer to that data to NULL because you don't own that memory chunk. Got it? :)

the words[k].word = NULL line is important just because you want to avoid any possible problem (like in the future if you accidently tried to access what it points to, NULL, the sytem would know that doesn't exist and prevent you). If you tried to access the piece of memory that the pointer points to (and you didn't set the pointer to NULL) the system WONT STOP YOU. That is why it is dangerous.

Moral of the story: don't point to anything you don't own :) So as soon as you free something, it is good practice to set the pointer NULL
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: DWW An array is just a pointer (think like object references in Java).

Hey, he'll get to that eventually. Don't confuse the poor boy ;)

The only thing he needs to think about is that the array actually represents enough memory for *numWords different wordType structs. The structs do actually exist right there, not in some random memory location referenced by words[x]. I think visualizing words[x] as a pointer is what through him off in the first place.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Holy crap, we've got to stop posting over top of eachother. Everytime I post I find 1 or 2 more posts that weren't there when I started typing.

Good explanation DWW. That was quite concisely put.
 

DWW

Platinum Member
Apr 4, 2003
2,030
0
0
Originally posted by: kamper
Holy crap, we've got to stop posting over top of eachother. Everytime I post I find 1 or 2 more posts that weren't there when I started typing.

Good explanation DWW. That was quite concisely put.

heh thanks. I'm really tired (just finished working on that Java project I posted in the other thread) and haven't touched C in awhile but it sounds pretty correct to my midnight eyes :)
 

DWW

Platinum Member
Apr 4, 2003
2,030
0
0
mattg1981 so does that clarify it more? :)

any more questions?

don't get put off by pointers, they are rather simple once you get the hang of them. even pointer arithmetic on multidimensional arrays is simple if you spend some time to read how they work. (which shouldn't take long...for me, I'd say I learned pointers in a Sunday afternoon read)
 

DWW

Platinum Member
Apr 4, 2003
2,030
0
0
Originally posted by: BingBongWongFooey
IIRC NULL is defined in stddef.h.

It doesn't really matter what its defined as though. I mean, there is no real use for knowing what its represented as on the current system, don't you think? Stick with using NULL and you will be okay :)
 

mattg1981

Senior member
Jun 19, 2003
957
0
76
you guys are awesome .. thanks for the very thorough explanation ... (I think you guys explained it better than my college proffesor did).
 

DWW

Platinum Member
Apr 4, 2003
2,030
0
0
Originally posted by: mattg1981
you guys are awesome .. thanks for the very thorough explanation ... (I think you guys explained it better than my college proffesor did).

PM me if you have anymore problems. I'm going to crash now cause I'm tired. Good luck with your work.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: DWW
Originally posted by: BingBongWongFooey
IIRC NULL is defined in stddef.h.

It doesn't really matter what its defined as though. I mean, there is no real use for knowing what its represented as on the current system, don't you think? Stick with using NULL and you will be okay :)

Yes, I agree, and that's exactly why I suggested including a header, instead of one defining it on their own.

#include <stddef.h> and you can NULL the night away without concern for what NULL's type is.

Though in C++ I'm pretty sure NULL is basically guaranteed to be just zero -- it's actually very common to just use 0 instead of NULL in C++. (but I'm just getting sidetracked here, ignore C++-isms in C :))