On Wed, Jan 28, 2009 at 04:39:39PM +0000, Andrew Haley wrote: > Zoltán Kócsi wrote: > >> No, this is since C90; nothing has changed in this area. NULL > >> doesn't mean "address 0", it means "nothing". The C statement > >> > >> if (ptr) > >> > >> doesn't mean "if ptr does not point to address zero", it means "if ptr > >> points to something". > > > > A question then: > > > > How can I make a pointer to point to the integer located at address > > 0x0? > > Didn't we address this before? You can't. > > > It is a perfectly valid object, it exists, therefore I should be > > able to get its address? In fact, if I have a CPU that starts its data > > RAM at 0, then the first data object *will* reside at address 0 and > > thus taking its address will result in a pointer that has all its bits > > clear. Obviously that pointer then should not be equal to NULL, since > > it was obtained by taking the address of a valid object, that is, the > > pointer indeed points to something. Therefore, > > > > int *a = &first_object; > > int *b = (int *) 0; > > > > must result in different values in a and b. Will it? > > Yes. > > "6.3.2.3 Pointers > > If a null pointer constant is converted to a pointer type, the > resulting pointer, called a null pointer, is guaranteed to compare > unequal to a pointer to any object or function." > > This implies that a linker cannot place an object at address zero.
Wrong. There is nothing which requires a null pointer to be all-bits-zero (even though that is by far the most common representation of null pointers.) Consider for example the following code sequence: int a; int *p, *np; a=0; p = (int*) a; /* a is not a null pointer constant */ np = (int *) 0; /* 0 is a null pointer constant */ After running that code 'np' will be a null pointer, while it is implementation defined if 'p' is a null pointer or not. It would be perfectly legal for a compiler to define that code to mean that 'p' is a pointer to address zero, while 'np' is a null pointer which internally happens to have the same representation as an int with the value 0xdeadbeef. (On such a system it would of course not be possible to place any real object at the address 0xdeadbeef since a null pointer does not point to any real object, but would seem unlikely to be a major problem.) -- <Insert your favourite quote here.> Erik Trulsson ertr1...@student.uu.se