address beyond array declaration

Discussion in 'C Programming' started by Douglas, Jun 17, 2004.

  1. Douglas

    Douglas Guest

    Hi,

    What is the need for the inaccessible pointer address beyond the end of an array?

    Eg. could

    for(ip = &array[0]; ip < &array[arraySize]; ip++)....

    not be rewritten as

    arraySize--;
    for(ip = &array[0]; ip <= & array[arraySize]; ip++)....


    Douglas
     
    Douglas, Jun 17, 2004
    #1
    1. Advertising

  2. Douglas

    Mike Wahler Guest

    "Douglas" <> wrote in message
    news:...
    > Hi,
    >
    > What is the need for the inaccessible pointer address beyond the end of an

    array?

    It's sometimes convenient when iterating through an array.
    But be sure not to try to dereference it.

    >
    > Eg. could
    >
    > for(ip = &array[0]; ip < &array[arraySize]; ip++)....
    >
    > not be rewritten as
    >
    > arraySize--;
    > for(ip = &array[0]; ip <= & array[arraySize]; ip++)....



    Yes, it could also be written e.g.:

    for(ip = &array[0]; ip <= & array[arraySize - 1]; ip++)....

    However the first form above is the most 'idomatic'
    (i.e. most quickly recognized by most C coders).

    Use whichever form most clearly expresses your intent.
    I don't like your second form, because if possible,
    I'd declare 'arraySize' as 'const' (typically it would
    be a function parameter), in which case the expression
    'arraySize--' would not be allowed.

    Personally, if I'm using an array's size as my
    'boundary condition' (as opposed to using an array
    element value, such as a string terminator), I'll use
    indices rather than pointers, e.g.:

    size_t i = 0
    for(i = 0; i < arraySize; ++i)
    /* etc */

    -Mike
     
    Mike Wahler, Jun 17, 2004
    #2
    1. Advertising

  3. Douglas

    Eric Sosman Guest

    Douglas wrote:
    > Hi,
    >
    > What is the need for the inaccessible pointer address beyond the end of an array?
    >
    > Eg. could
    >
    > for(ip = &array[0]; ip < &array[arraySize]; ip++)....
    >
    > not be rewritten as
    >
    > arraySize--;
    > for(ip = &array[0]; ip <= & array[arraySize]; ip++)....


    Yes, you could write this and it would work. But it
    still uses "one past the end" pointer: What is the value
    of `ip' when the test fails?

    --
     
    Eric Sosman, Jun 17, 2004
    #3
  4. Douglas wrote:
    > ...
    > What is the need for the inaccessible pointer address beyond the end of an array?
    >
    > Eg. could
    >
    > for(ip = &array[0]; ip < &array[arraySize]; ip++)....
    >
    > not be rewritten as
    >
    > arraySize--;
    > for(ip = &array[0]; ip <= & array[arraySize]; ip++)....
    > ...


    Firstly, not in case when 'arraySize' is initially zero.

    Secondly, your version still produces a pointer that points beyond the
    end of an array.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 18, 2004
    #4
  5. Douglas

    JV Guest

    "Mike Wahler" <> wrote in message
    news:_eiAc.15239$...
    > "Douglas" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > What is the need for the inaccessible pointer address beyond the end of

    an
    > array?
    >
    > It's sometimes convenient when iterating through an array.
    > But be sure not to try to dereference it.
    >

    BTW why the later sizeof (a) causes segmentation fault in the code below? I
    guessed that if would have resulted still 5*sizeof (int). Now I just found
    out that there really is difference between arrays and pointers that
    matters:). Althought in practice I use arrays if the length is fixed and
    just declare a pointer if I the number of elements varies runtime.
    -Jyrki


    int a[5];
    int main(void){
    printf("%i\n",sizeof(int));

    printf("%i\n",sizeof(a));
    realloc(a,10*sizeof(int));
    printf("%i\n",sizeof(a));
    }

    -jyrki
     
    JV, Jun 18, 2004
    #5
  6. Douglas

    Richard Bos Guest

    "JV" <> wrote:

    > BTW why the later sizeof (a) causes segmentation fault in the code below?


    It doesn't. That is, that may be the line that triggers the segfault,
    but the actual cause is...

    > int a[5];
    > int main(void){
    > printf("%i\n",sizeof(int));
    >
    > printf("%i\n",sizeof(a));
    > realloc(a,10*sizeof(int));


    ....here. You cannot use realloc() on memory you didn't get from *alloc()
    in the first place. In this case, you cannot use realloc() on a, which
    is a static file-scope array.
    This call invokes undefined behaviour; after it is executed, _nothing_
    can be relied on. In theory, your program is now allowed to send love
    letters to your supervisor. In practice, you've probably corrupted your
    allocation arena...

    > printf("%i\n",sizeof(a));


    ....leading to a segfault next time you refer to this object, or use
    another allocation function. But this line _in itself_ is not the
    problem.

    Richard
     
    Richard Bos, Jun 18, 2004
    #6
  7. "JV" <> wrote:
    <snip>

    >BTW why the later sizeof (a) causes segmentation fault in the code below? I
    >guessed that if would have resulted still 5*sizeof (int). Now I just found
    >out that there really is difference between arrays and pointers that
    >matters:).

    <snip>

    >int a[5];
    >int main(void){
    > printf("%i\n",sizeof(int));
    >
    > printf("%i\n",sizeof(a));
    > realloc(a,10*sizeof(int));
    > printf("%i\n",sizeof(a));
    >}


    There's one more /very/ important difference: any attempt to
    reallocate an array results in undefined behaviour, of which
    the segfault you observed is only one incarnation.

    BTW: your code fails to #include two necessary standard headers,
    and the invocation of realloc would still be horribly wrong, even
    if the first argument were a pointer previously returned by one
    of the *alloc functions. Plus, omitting the return statement
    in main is only sanctioned by ISO C99.

    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
     
    Irrwahn Grausewitz, Jun 18, 2004
    #7
    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. Noah
    Replies:
    5
    Views:
    962
  2. Alex Vinokur
    Replies:
    4
    Views:
    595
    Jonathan Turkanis
    Apr 5, 2004
  3. candide
    Replies:
    65
    Views:
    1,410
  4. John Koleszar

    address of static array element as address constant

    John Koleszar, Jul 7, 2008, in forum: C Programming
    Replies:
    7
    Views:
    625
    John Koleszar
    Jul 15, 2008
  5. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,150
    Keith Thompson
    Sep 20, 2011
Loading...

Share This Page