Re: validity of a pointer



pete wrote:

Wolfgang Draxinger wrote:

First read the pointer FAQ. Then you must understand, that the
only "invalid" pointer in terms of C is the null pointer, with
the value 0.

An indeterminate pointer is more invalid than a null pointer.

In terms of C a null pointer is not something invalid in the term, that it
may not be there. However dereferencing a null pointer is invalid, thus I
wrote the word "invalid" in quotations marks, to designate it as being
meant in the OPs intention.

Any pointer with a value not being null can be dereferenced legally, as long
as we'te talking in terms of the language.

In terms of a specific implementation there might be further values, that
are invalid for a given pointer type (e.g. pointer values are alignment
constrained).

And then there is the classification of a pointer being valid or not by the
(operating system). Today one can cheaply build a 32 bits machine with 4GiB
memory (costs about 100$). Without a operating system, memory protection
and a paging system the physical memory can be addressed linearily. In this
situation, 32 bit CPU and 4GiB of memory running a single program in
physically addressing mode, virtually every pointer can be dereferenced
without raising a bus error exception.

But the C language deliberately doesn't specify anything about the low level
details that are implied by pointers. The only thing that's granted is,
that a pointer with the value 0 cannot be dereferenced and thus may be
considered invalid.

Wolfgang

P.S.: That I gave some detailed information on how to check a pointer for
validity under Linux is, that there's IMHO no sensible newsgroup for such
questions. The closest match would be comp.os.linux.development.apps, but
the guys there are focused on things like GUI and stuff like that. The
other Linux devel NG is comp.os.linux.development.system, which focuses on
the kernel and there a whole set of other rules apply.

.



Relevant Pages

  • Re: Simple question, err... I think
    ... One assumption is then that client code should leak memory. ... Given that there's no deallocation for these RB trees, ... a pointer to an invalid tree and it might segfault. ...
    (comp.programming)
  • Re: doubt on char *
    ... SGI IRIX64 does not catch dereferencing a NULL pointer. ... So suddenly the memory page that covered ...
    (comp.lang.c)
  • 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: access_ok macor
    ... The problem which I have is that if I run socketpair, getsockname, getpeername LTP ... tests with invalid salen pointer there are addresses close to 0x0. ... This sounds like a classic NULL pointer dereference that is handled correctly ... err = get_user; ...
    (Linux-Kernel)
  • Re: segmentation fault in strcmp()
    ... What is the meaning of such an error like invalid next size/ ... It means that the freeis trying to figure out the size of the next block of memory in the heap; information it needs in order to complete the process of freeing the memory you've asked it to release. ... Since the place where it looks is determined in part by the pointer that you pass to free, one possibility is that you're passing the wrong pointer to free; ... If p is not a pointer returned by a call to malloc() or realloc), then freecould cause this problem. ...
    (comp.lang.c)