Arrays and pointers

T

Tony Johansson

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

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
 
J

Joona I Palaste

Tony Johansson said:
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);

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

--
/-- Joona Palaste ([email protected]) ---------------------------\
| 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
 
P

pete

Tony said:
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.
 
D

Deepak

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 said:
Tony said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top