Arrays and pointers

Discussion in 'C Programming' started by Tony Johansson, Aug 26, 2003.

  1. 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
     
    Tony Johansson, Aug 26, 2003
    #1
    1. Advertising

  2. Tony Johansson <> 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);

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

    --
    /-- Joona Palaste () ---------------------------\
    | 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
     
    Joona I Palaste, Aug 26, 2003
    #2
    1. Advertising

  3. Tony Johansson

    pete Guest

    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
     
    pete, Aug 26, 2003
    #3
  4. Tony Johansson

    Deepak Guest

    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 <> wrote in message news:<>...
    > 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.
     
    Deepak, Sep 3, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    387
  2. Replies:
    1
    Views:
    655
    -berlin.de
    Mar 28, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,135
    Philipp
    Jan 20, 2009
  4. Francesco
    Replies:
    2
    Views:
    1,115
    Francesco
    Nov 6, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
Loading...

Share This Page