On Friday 22 April 2005 11:40, Synthetic Cartoonz wrote:
On Thursday 21 April 2005 20:50, Colin Carter wrote:
On Friday 22 April 2005 03:37, synthetoonz@bellsouth.net wrote:
From: Colin Carter
[snip]
How do you write that a pointer ptr is pointing nowhere?
If you want to know if a pointer is NULL, that is, not a valid pointer, you just compare the pointer. If you dereference the pointer (that's the * ) then you are examining what the pointer is pointing at.
Explicitly stated, if (pointer == NULL) /* The pointer must be NULL */
Thanks Synthetooz and Jerry. I usually see Jerry's notation, but am comfortable with, and actually prefer, the if( !pointer).
Anyway, you are both really saying that the memory where pointer value is stored contains a full house of zeros and one just tests for zero.
Which in turn means that I ought to be able to say
pointer = 0; but I doubt if the C compiler would allow me to be so simple.
A pointer is just the numeric address of a memory location . Starting at 0 and going up from there . (I've never seen an architecture that specifies negative addresses, though I guess anything is possible.) In most implementations assigning the integer value 0 to a pointer produces the expected result. Typically, the 0 (as integer) is promoted to whatever storage unit a pointer is. You may see a compiler warning about the type conversion.
More correctly, you should demonstrate to the compiler that you know what you're doing and eliminate any type conversion warning by typecasting the literal value 0 to the same pointer type...
SomeExoticStructure * myFancyPointer;
myFancyPointer = (SomeExoticStructure *)0;
(This assumes that in C one never wishes to access the value in location 0.
Yes, but computer architectures have been fairly consistent about 0 being something user programs shouldn't access, with 0 being reserved for the system or otherwise outside the allowed memory map of a user process. Simply reading whatever is in location 0 is sufficient to crash a program on many kinds of computers.
A more invalid" address would be -1, but that would cause havoc with if(). )
-1 promoted to an address could very well be a valid memory address.
pointer = &K; means pointer holds the address of K
Yes.
So I suppose, from your notes, I should write pointer = NULL;
Yes.
because wouldn't *pointer = NULL; imply that I am pointing to an (integer) whose value is zero?
You are assigning NULL (that is, zero) to whatever the contents of pointer points at. So, assuming pointer contains the address of K (from earlier), then *pointer = NULL assigns NULL (or zero promoted to the compatible integer type) to K.
You can see that I am not that comfortable with C pointers. I can understand your if(...) notes, but I can't quite see how one is formally supposed to 'de-allocate' the pointer.
Simple pointer gymnastics like what we've been discussing above does not require allocation and freeing that which is referenced by pointers.
Allocation and free memory at run time is another big subject that depends on pointers. When you explicitly declare data in your programs, the compiler is creating the storage (usually on the heap, maybe the stack) for you. You can declare all the pointers to those types/values you want and assign and re-assign the pointers until you're blue in the face. You only need to start worrying about allocation and freeing memory when you start creating storage for types on the fly during program execution.
Thanks for all that. I feel a lot more comfortable now. I always kind of blundered my way around with "try, try again" until it works. I'll keep your notes. There will be no response from me for a couple of days as I am off to see the radio telescopes at Siding Springs. http://www.mso.anu.edu.au/home.php As we Aussies say: 'aveagoodweekend Colin