Re: end of array
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 05 Mar 2007 15:31:28 +0000
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 haveYou can make a pointer point to anything. As to how useful it is...well
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 ?
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
.
- Follow-Ups:
- Re: end of array
- From: Daniel Rudy
- Re: end of array
- References:
- end of array
- From: subramanian100in@xxxxxxxxx, India
- Re: end of array
- From: Daniel Rudy
- Re: end of array
- From: santosh
- end of array
- Prev by Date: Re: C container and persistent library ?
- Next by Date: Re: A base conversion~ help me to correct it since it can't run
- Previous by thread: Re: end of array
- Next by thread: Re: end of array
- Index(es):
Relevant Pages
|