Re: references and pointers

From: James Dennett (jdennett_at_acm.org)
Date: 02/23/04


Date: Sun, 22 Feb 2004 17:52:43 -0800

Sam of California wrote:
> "Artie Gold" <artiegold@austin.rr.com> wrote in message
> news:c0f5l9$15q5j5$1@ID-219787.news.uni-berlin.de...
>
>>A reference cannot be NULL (so you don't have to check for that to
>>program robustly).
>
>
> So is the following invalid?

Your code is "invalid" -- specifically, it invokes
undefined behavior by referencing a null pointer.

> My VC 6 compiler accepts it; the value it
> outputs is "00000000" for "&a".

VC6 is quite allowed to do that, or to print
"error in program", or to do anything else.

>
> // - - - - - - - - -
> void Referrence(int &a) {
> cout << "Address: " << &a << '\n';
> return;
> }

That's a perfectly reasonable function, modulo
the spelling mistake in its name, which will
write the address of an int to cout (presumably
a stream, most likely ::std::cout).

> // - - - - - - - - -
>
> int main(int argc, char* argv[], char *envp[]) {

Note that the envp part of this is a non-standard
extension, and not really needed; std::getenv can
be used to read from the environment.

> int &r = *((int *)NULL);

There you dereference a null pointer, which is not
permitted. Anything goes.

> Referrence(r);
> return 0;
> }

Hope this clarifies -- there is no way to get a "null"
reference in a valid program. By the time your check
for a "null" reference might catch something, it's too
late.

-- James.



Relevant Pages

  • Re: null references
    ... To say that null and invalid references don't exist suggests that if you would just change to the function would be unable to fail and therefore correctly manages the semaphore, but this is a bogus argument. ... The reference version of the function can still throw an access violation and still needs to be coded for exception safety. ... If you define the above function as taking a reference arg then the undefined behaviour occurs the moment someone tries to dereference an invalid pointer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: use delete to destroy primitive/object types but memory is not freed
    ... >> invokes undefined behavior, even if you don't dereference it. ... Recall that g is a pointer to double. ... the operating system or some other standard. ... > But you said, "Also, any reference to g, the pointer variable, ...
    (comp.lang.c)
  • Re: Question on LSP
    ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... Because construction paradigms have different goals. ... But that has nothing to do with what a pointer type ... But that does not mean that the semantics of an object reference is ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Modifying Class Object
    ... Before I start, note we're talking about semantics, not ... For example, as I used as reference in my first posting, the Java language spec. ... A pointer stores a reference to something. ...
    (comp.lang.python)