undefined behavior

Discussion in 'C Programming' started by deepak, Jan 12, 2007.

  1. deepak

    deepak Guest

    Using 'char' as an array index is an undefined behavior?
     
    deepak, Jan 12, 2007
    #1
    1. Advertising

  2. deepak

    Zara Guest

    On 12 Jan 2007 06:23:47 -0800, "deepak" <> wrote:

    >Using 'char' as an array index is an undefined behavior?


    No. the char will be promoted to int.
     
    Zara, Jan 12, 2007
    #2
    1. Advertising

  3. deepak wrote:

    > Using 'char' as an array index is an undefined behavior?


    No, though it might not be the best idea, as that limits you to being
    able to (portably) index only 128 elements of the array.

    --
    Clark S. Cox III
     
    Clark S. Cox III, Jan 12, 2007
    #3
  4. deepak writes:
    > Using 'char' as an array index is an undefined behavior?


    No, but if 'char' is 8-bit and signed, and contains an 8-bit character,
    then it is negative. So

    static char foo[256];
    void bar(char ch) { ... foo[ch] ... }
    can use a negative index to foo[], which yields undefined behavior.
    Use foo[(unsigned char)ch] instead.

    --
    Hallvard
     
    Hallvard B Furuseth, Jan 12, 2007
    #4
  5. In article <>,
    Hallvard B Furuseth <> wrote:

    >> Using 'char' as an array index is an undefined behavior?


    >No, but if 'char' is 8-bit and signed, and contains an 8-bit character,
    >then it is negative.


    Which is why some compilers (e.g. gcc) warn you about it.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jan 12, 2007
    #5
  6. deepak said:

    > Using 'char' as an array index is an undefined behavior?


    No, but indexing outside the bounds of an array does invoke undefined
    behaviour. If the value stored in the char is negative (which it can be if
    char is signed by default) or greater than or equal to the number of
    elements in the array, the behaviour is undefined.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jan 12, 2007
    #6
  7. "Richard Heathfield" <> wrote in message
    news:...
    > deepak said:
    >
    >> Using 'char' as an array index is an undefined behavior?

    >
    > No, but indexing outside the bounds of an array does invoke undefined
    > behaviour. If the value stored in the char is negative (which it can be if
    > char is signed by default) or greater than or equal to the number of
    > elements in the array, the behaviour is undefined.


    int array[10] = { 0,1,2,3,4,5,6,7,8,9};

    int *p = &array[4];

    signed char index = -1; // signed for clarity

    printf("%d\n", p[index]);



    are you guys saying that any negative index is undefined behaviour or just
    that if you dereference before &array[0]?
     
    Serve Laurijssen, Jan 12, 2007
    #7
  8. Serve Laurijssen wrote:
    > "Richard Heathfield" <> wrote in message
    > news:...
    >> deepak said:
    >>
    >>> Using 'char' as an array index is an undefined behavior?

    >> No, but indexing outside the bounds of an array does invoke undefined
    >> behaviour. If the value stored in the char is negative (which it can be if
    >> char is signed by default) or greater than or equal to the number of
    >> elements in the array, the behaviour is undefined.

    >
    > int array[10] = { 0,1,2,3,4,5,6,7,8,9};
    >
    > int *p = &array[4];
    >
    > signed char index = -1; // signed for clarity
    >
    > printf("%d\n", p[index]);
    >
    >
    >
    > are you guys saying that any negative index is undefined behaviour or just
    > that if you dereference before &array[0]?


    Indexing an *array* with a negative index is undefined behavior, because
    negative indices are, by definition, out of bounds. As you have
    demonstrated, indexing a *pointer* with a negative index might not be;
    because that pointer might actually point into a larger array.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Jan 12, 2007
    #8
    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. aka
    Replies:
    10
    Views:
    709
  2. REH
    Replies:
    25
    Views:
    832
    Victor Bazarov
    Mar 29, 2005
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,766
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    607
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    373
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page