J
James Kanze
|> > Because on some machine architectures, pointers may be validated
|> > at the time that they are loaded into a machine register instead
|> > of being validated each time they attempt to access memory.
|> I have heard that a lot before, but never ran across such
|> architecture. Could you at least give me one example?
Some implementations of C on the 80286.
|> Let's assume this is the case: is NULL a valid pointer? I don't
|> think so. It can't be dereferenced.
It is a valid pointer value. That doesn't mean that you can dereference
it. One past the end pointers are also valid pointer values.
|> Yet, it is clearly impossible that the following statement be not
|> valid or undefined behavior, because 1. it would make pointer
|> programming very difficult and 2. it would cause havoc in trillions
|> of lines of code all over the world:
|> p = NULL;
|> What do you think? Do you think that the "NULL" pointer is a special
|> case on the aforementioned architectures and therefore it's still ok
|> to assign it to some pointer variable?
It's up to the implementation, but they must do something to ensure that
loading a NULL pointer will not trigger an exception.
Back when the 80286 was a recent architecture, most of the
implementations I used mapped the page 0 to a page full of 0's, so that
dereferencing the null pointer actually worked. If they did this, of
course, there was no problem copying a null pointer.
|> > at the time that they are loaded into a machine register instead
|> > of being validated each time they attempt to access memory.
|> I have heard that a lot before, but never ran across such
|> architecture. Could you at least give me one example?
Some implementations of C on the 80286.
|> Let's assume this is the case: is NULL a valid pointer? I don't
|> think so. It can't be dereferenced.
It is a valid pointer value. That doesn't mean that you can dereference
it. One past the end pointers are also valid pointer values.
|> Yet, it is clearly impossible that the following statement be not
|> valid or undefined behavior, because 1. it would make pointer
|> programming very difficult and 2. it would cause havoc in trillions
|> of lines of code all over the world:
|> p = NULL;
|> What do you think? Do you think that the "NULL" pointer is a special
|> case on the aforementioned architectures and therefore it's still ok
|> to assign it to some pointer variable?
It's up to the implementation, but they must do something to ensure that
loading a NULL pointer will not trigger an exception.
Back when the 80286 was a recent architecture, most of the
implementations I used mapped the page 0 to a page full of 0's, so that
dereferencing the null pointer actually worked. If they did this, of
course, there was no problem copying a null pointer.