473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Arrays and pointers

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


Nov 13 '05 #1
3 1595
Tony Johansson <jo*****************@telia.com> 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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #2
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
Nov 13 '05 #3
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.

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
1
by: Nathan Gilbert | last post by:
I have a function that is returning a 2D array (declared using double pointers) and I want to be able to append a row of data to the beginning and end of this array without having to create a new...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.