Velocity Reviews > Arrays and pointers

# Arrays and pointers

Tony Johansson
Guest
Posts: n/a

 08-26-2003
Hello experts!

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);

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.

//Tony

Joona I Palaste
Guest
Posts: n/a

 08-26-2003
Tony Johansson <(E-Mail Removed)> scribbled the following:
> Hello experts!

This is not a chat room. Please don't keep reposting your message just
because no one has replied within 5 seconds. We will reply eventually
even if you only post one message.

> 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.

Actually forming an illegal address causes undefined behaviour, which
means that your comparison may succeed, fail, segfault, or turn your
hard drive into a jar of raspberry marmalade.

> for (ptr = &a[num-1]; 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.

Not necessarily. There are platforms where such address don't even
exist. The safe way to do your for loop would be:

signed int i;
for (i=num-1; i>=0; i--)
printf("%i\n", a[i]);

This is because for ints, the value -1 is guaranteed to be valid.

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You will be given the plague."
- Montgomery Burns

pete
Guest
Posts: n/a

 08-26-2003
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.

--
pete

Deepak
Guest
Posts: n/a

 09-03-2003
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

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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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.