Re: end of array



santosh wrote, On 05/03/07 15:14:
Daniel Rudy wrote:
At about the time of 3/3/2007 8:57 PM, subramanian100in@xxxxxxxxx, India
stated the following:
Suppose we have
char array[10];

C allows taking the address of array[10] (ie &array[10] is valid)
though the element array[10] cannot be accessed.

Is this allowed for use in binary search method (as given in K & R
second edition page 137) or is there any other reason ?

You can make a pointer point to anything. As to how useful it is...well
that's up to you.

I can do this:

char a[10], *p;
p = &a[65536];

No you can't. Or rather you can, but since you are doing pointer arithmetic that goes more than 1 past the end of the object the pointer arithmetic invokes undefined behaviour and anything can happen. Note that the [] operator is defined in terms of pointer arithmetic.

p is a pointer, it does point to something, but if I try to use it, then
I will probably get a segmentation fault or a memory fault. In either
case, the program will dump core.

Not necessarily. The behaviour is undefined if you happen to access
memory you don't own. Undefined means anything can happen. Under
modern memory protected OSen, the programme will, as you note, be
terminated, but under other systems, it could very well keep running,
but yield wrong results or a system hangup.

You don't have to attempt to dereference the pointer to invoke undefined behaviour. Merely calculating a pointer before or more than 1 past the end of an object invokes undefined behaviour even if you do nothing with it. Of course, dereferencing a pointer that does not point to a C object also invokes undefined behaviour that often behaves as you describe, however it is always possible (as far as the C standard is concerned) that instead it will make daemons fly out of your nose.
--
Flash Gordon
.



Relevant Pages

  • Re: Pointer and Its memory
    ... I dereference the pointer A using free. ... That is deallocating the memory it points at. ... Note that heap memory is an implementation detail, and not all systems even have a heap. ...
    (comp.lang.c)
  • Re: Confused about "void **"
    ... You cannot dereference a `void*' because ... memory of unknown size and significance. ... And what kind of a pointer points to a `void*'? ... of data object, ...
    (comp.lang.c)
  • Re: Validate a pointer
    ... >> damage has been done. ... > the pointer - the memory you trash will be the memory of the child ... My point was that by the time you decide to dereference your possibly ... invalid pointer, lots of data might have be trampled on. ...
    (comp.unix.programmer)
  • Re: Malloc code
    ... to null when the associated memory is released. ... If you accidently try to dereference the pointer after it has ... On embedded systems with no mmu, ...
    (microsoft.public.vc.language)
  • Re: end of array
    ... You can make a pointer point to anything. ... arithmetic invokes undefined behaviour and anything can happen. ... I will probably get a segmentation fault or a memory fault. ... strip view finger mount fcsk more fcsk yes spray umount sleep ...
    (comp.lang.c)