From: Colin Carter colincarter@exemail.com.au On Thursday 21 April 2005 22:31, Randall R Schulz wrote:
On Thursday 21 April 2005 02:48, Michael Stevens wrote:
In C++ it would seem that the 'const void * NULL = 0' definition would be a good thing as opposed to the literal '0' which Bjarne is recommending.
I used these routinely in my C++ work:
const void *NIL = 0; const char NUL = 0;
I would use a naked 0 in source code only where the context was actually integer, not pointer or character, in which case I'd use on of these.
I like this idea - it makes it clear about what you mean, whereas (to me anyway) there is always confusion with NULL which is not even NUL.
How do you write that a pointer ptr is pointing nowhere? That is, the value of the pointer is zero. I mean so that it is clear that it is not pointing to a zero value. Pardon my ignorance, but my preference is for FORTRAN and we are positively discouraged from using pointers because all of our variables are in fact addresses (pointers) and not 'values' as in C.
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 Thursday 21 April 2005 1:37 pm, synthetoonz@bellsouth.net wrote:
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 the ( !pointer ) and it means the same thing
as
above.
This works in nearly all implementations, and I certainly use it. But, it is technically improper. The "(pointer == NULL)" expression is much more readable, especially to some programmers who might be less strong in C or C++. But, the "( !pointer )" does avoid, as you pointer out the single vs. double "=" issue. (But, I'm being somewhat pedantic today after a late BLU meeting and an early start at work on top of giving blood).
On Friday 22 April 2005 03:37, synthetoonz@bellsouth.net wrote:
From: Colin Carter colincarter@exemail.com.au
On Thursday 21 April 2005 22:31, Randall R Schulz wrote:
On Thursday 21 April 2005 02:48, Michael Stevens wrote:
In C++ it would seem that the 'const void * NULL = 0' definition would be a good thing as opposed to the literal '0' which Bjarne is recommending.
I used these routinely in my C++ work:
const void *NIL = 0; const char NUL = 0;
I would use a naked 0 in source code only where the context was actually integer, not pointer or character, in which case I'd use on of these.
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 */
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:
On Friday 22 April 2005 03:37, synthetoonz@bellsouth.net wrote:
From: Colin Carter colincarter@exemail.com.au
[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.
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 colincarter@exemail.com.au
[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
On Thursday 21 April 2005 9:40 pm, Synthetic Cartoonz 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.
Pointers are generally unsigned. Many architectures do use hexadecimal 'F" in high-order positions.
Most architectures do not permit user mode addresses at 0, but some do.
On Thursday 21 April 2005 8:50 pm, Colin Carter wrote:
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.
This is correct. many C and C++ functions return pointers, and a return value of NULL indicates that some kind of error occurred.
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.
programming@lists.opensuse.org