The local variables in a function are allocated on to process stack.
so if your functions have the following locals:
int a;
int b;
int c;
then all the variables in the stack will be placed upside down.
U might be knowing that stack grows to lower address, so it means that
"a" will be at the highest address, then "b" will be placed and finally "c".
As shown below:
NOT VALID LOCATIONS
-----------|Lower address : top of stack
c: 4 bytes
-----------|
b: 4 byte
-----------|
a:4 byte
-----------|Highest address
So the last variable c is on the top of the stack.
Suppose this happens to be an array. then the memory space will be
allocated for it at top of the stack.
And if u try to read or write something with negative index, that memory access
will be in the area marked in above diagram as "NOT VALID LOCTIONS".
Anything above top of the stack is not valid.
So it may cause a memory corruption as that area is not allocated for.
DG
pete <pf*****@mindspring.com> wrote in message news:<3F***********@mindspring.com>...
Tony Johansson wrote:
Hello experts!
I question about understanding. I have read a book that says.
If you have an array of integers you know that one element
past the end of
the array is legal as address but not to follow. That's ok.
The address to one element past the end of the array is
also grater then the
address to the last element.That's also ok.
The book says " Don't assume that a pointer to the
element just before the
start of an array is legal.
This for loop should be valid as I think but
the book say no because this
for loop has a problem:
the loop terminates only when ptr becomes less then a. And that's not
guaranteed to be a legal address,
which means the comparasion may fail.
for (ptr = &a[num-1]; ptr >= a; ptr--)
printf("%i\n", *ptr);
/* Write that, this way: */
ptr = a + num;
while (ptr != a) {
--ptr;
printf("%i\n", *ptr);
}
I think, don't you that the address to the
element just before the start of
an array must be lower then the address to the first element.
The address of the first element
might be the lowest address available.