Re: Re: [suse-programming-e] Programming standards!
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 */ The joy of ! (that's NOT) is that it helps do the same sort of check with less typing. So, you could say if ( !pointer ) and it means the same thing as above. ! can save you from that dreaded = vs == typo where you inadvertently write code that assigns a value instead of compares. A random function that protects itself from being passed a 0 (that's NULL pointer)... char * foo(int x, char * bar) { if ( !bar ) /* pointer must be zero */ return NULL; /* .... blah blah .... */ return somethingnotzero; } elsewhere some code calls foo()... ... char * mybar = NULL; /*Maybe mybar is assigned something later, or maybe not */ if (!foo(2, mybar) ) { /* foo returned NULL, so it is an error */ /* do something about the error */ } else { /* I like success. foo worked. */ } Very common in code is something that checks if the pointer is (not) NULL and that what it points to is also (not) NULL (or zero) . i.e. you want to know that a string pointer is valid and that it points to a string with at least one real character in it.... char * p = NULL; /*.... maybe we made p point to a string or not.... */ if ( !p || !*p ) printf ("There is no P!\n");
On Friday 22 April 2005 03:37, synthetoonz@bellsouth.net wrote:
snip 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. (This assumes that in C one never wishes to access the value in location 0. A more invalid" address would be -1, but that would cause havoc with if(). ) pointer = &K; means pointer holds the address of K So I suppose, from your notes, I should write pointer = NULL; because wouldn't *pointer = NULL; imply that I am pointing to an (integer) whose value is zero? 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. Regards, Colin
On Thursday 21 April 2005 20:50, Colin Carter wrote:
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.
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.
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.
On Friday 22 April 2005 11:40, Synthetic Cartoonz wrote:
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
On Thursday 21 April 2005 9:40 pm, Synthetic Cartoonz wrote:
Most architectures do not permit user mode addresses at 0, but some do. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Thursday 21 April 2005 8:50 pm, Colin Carter wrote:
An example of this is the fgets function: char *fgets(char *s, int size, FILE *stream); "fgets() returns s on success, and NULL on error or when end of file occurs while no characters have been read". Note that in general, Unix and Linux functions usually return 0 (integer 0) on success and usually -1 on failure. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Friday 22 April 2005 03:37, synthetoonz@bellsouth.net wrote:
snip 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. (This assumes that in C one never wishes to access the value in location 0. A more invalid" address would be -1, but that would cause havoc with if(). ) pointer = &K; means pointer holds the address of K So I suppose, from your notes, I should write pointer = NULL; because wouldn't *pointer = NULL; imply that I am pointing to an (integer) whose value is zero? 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. Regards, Colin
On Thursday 21 April 2005 20:50, Colin Carter wrote:
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.
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.
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.
On Friday 22 April 2005 11:40, Synthetic Cartoonz wrote:
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
On Thursday 21 April 2005 9:40 pm, Synthetic Cartoonz wrote:
Most architectures do not permit user mode addresses at 0, but some do. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Thursday 21 April 2005 8:50 pm, Colin Carter wrote:
An example of this is the fgets function: char *fgets(char *s, int size, FILE *stream); "fgets() returns s on success, and NULL on error or when end of file occurs while no characters have been read". Note that in general, Unix and Linux functions usually return 0 (integer 0) on success and usually -1 on failure. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
participants (4)
-
Colin Carter
-
Jerry Feldman
-
Synthetic Cartoonz
-
synthetoonz@bellsouth.net