Unconstrained behavior in arrays.

Discussion in 'C Programming' started by Chad, Oct 13, 2005.

  1. Chad

    Chad Guest

    Can someone tell my why:

    char i;
    int a;

    Causes unconstrained behavior.

    Thanks in advance.


    Chad
     
    Chad, Oct 13, 2005
    #1
    1. Advertising

  2. "Chad" <> wrote in news:1129164549.280165.266960
    @z14g2000cwz.googlegroups.com:

    > Can someone tell my why:
    >
    > char i;
    > int a;
    >
    > Causes unconstrained behavior.


    I don't know what you have in mind when you say 'unconstrained behavior'.
    I have not heard that expression before.

    D:\Home> cat t.c
    char i;
    int a;

    D:\Home> gcc -Wall -std=c99 -pedantic -c t.c
    t.c:2: error: variable-size type declared outside of any function

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)
    http://groups.google.com/support/bin/answer.py?answer=14213
     
    A. Sinan Unur, Oct 13, 2005
    #2
    1. Advertising

  3. "Chad" <> writes:
    > Can someone tell my why:
    >
    > char i;
    > int a;
    >
    > Causes unconstrained behavior.


    Do you mean "undefined behavior"?

    The code fragment you posted doesn't cause any behavior at all, since
    it's not a legal C translation unit. Show us a complete translation
    unit, or preferably a complete program, and we can discuss it.

    Or just ask yourself this question: what is the value of i?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 13, 2005
    #3
  4. Chad

    Chad Guest

    I misworded it. The question stems from the following thread:

    http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0

    One of the responses was:

    See below.


    >#include <stdio.h>


    >void Func1()


    Change this to:

    void Func1( void )

    to match the pointer declarations below.



    >{
    > printf("func1\n");
    > return;
    >}


    >void Func2()


    Same here.

    >{
    > printf("func2\n");
    > return;
    >}


    >void Func3()


    Same here.

    >{
    > printf("func3\n");
    > return;
    >}


    >void StartFunctions( void (*f)(void) )
    >{
    > printf("Starting Function...\n");
    > f();
    > return;
    >}


    >int main()


    This isn't one of the two legitimate declarations of main, use:

    int main( void )


    >{
    > char i;


    Using a character value for array subscripting is at least suspicious;
    no, wait, it's actually a constraint violation. Use:

    int i;


    > void *v[] = { (void*)Func1, (void*)Func2, (void*)Func3 };



    In order to avoid pointer type mismatches you presumably want:

    void (*v[])(void) = { Func1, Func2, Func3 };

    Note: cluttering your code with spurious casts is the wrong approach.
    Better use correct declarations in the first place.

    > for ( i = 0; i < 3; i++ ) StartFunctions( v );


    Minor quibble: beware of magic numbers (3).

    > return 0;


    That's fine. :eek:)

    >}


    The question I meant to ask was why does 'char i' cause a constraint
    violation, but 'int i' wouldnt?

    Chad
     
    Chad, Oct 13, 2005
    #4
  5. Chad

    Ben Pfaff Guest

    "Chad" <> writes:

    > Can someone tell my why:
    >
    > char i;
    > int a;
    >
    > Causes unconstrained behavior.


    In C90, this is a constraint violation, because the expression
    that indicates the size of the array is not an integral constant
    expression.

    In C99, at file scope this is a constraint violation for the same
    reason. At block scope it yields undefined behavior because `i'
    is uninitialized.
    --
    A competent C programmer knows how to write C programs correctly,
    a C expert knows enough to argue with Dan Pop, and a C expert
    expert knows not to bother.
     
    Ben Pfaff, Oct 13, 2005
    #5
  6. "Chad" <> writes:
    > I misworded it. The question stems from the following thread:
    >
    > http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0
    >
    > One of the responses was:

    [snip]
    >>{
    >> char i;

    >
    > Using a character value for array subscripting is at least suspicious;
    > no, wait, it's actually a constraint violation. Use:
    >
    > int i;

    [...]
    > The question I meant to ask was why does 'char i' cause a constraint
    > violation, but 'int i' wouldnt?


    Ok, that's completely different from what you posted. In your
    original post, you were using i as the dimension in an array
    declaration; here, you're just using it as an array index.

    Using a char value as an array index does not violate a constraint.
    The constraints on an array subscripting expression are (with
    underscores denoting italics):

    One of the expressions shall have type "pointer to object _type_",
    the other expression shall have integer type, and the result has
    type "_type_".

    Since char is an integer type, it's perfectly legal to use a char as
    an array index.

    It is suspicious, though. It's implementation-defined whether char is
    signed or unsigned, and the value is promoted to int (or conceivably
    to unsigned int) anyway. The space saving for a single declared
    object is insignificant. There's almost never a good reason to use a
    char as an array index.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 13, 2005
    #6
  7. Chad

    Joe Wright Guest

    Keith Thompson wrote:
    > "Chad" <> writes:
    >
    >>I misworded it. The question stems from the following thread:
    >>
    >>http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0
    >>
    >>One of the responses was:

    >
    > [snip]
    >
    >>>{
    >>> char i;

    >>
    >>Using a character value for array subscripting is at least suspicious;
    >>no, wait, it's actually a constraint violation. Use:
    >>
    >>int i;

    >
    > [...]
    >
    >>The question I meant to ask was why does 'char i' cause a constraint
    >>violation, but 'int i' wouldnt?

    >
    >
    > Ok, that's completely different from what you posted. In your
    > original post, you were using i as the dimension in an array
    > declaration; here, you're just using it as an array index.
    >
    > Using a char value as an array index does not violate a constraint.
    > The constraints on an array subscripting expression are (with
    > underscores denoting italics):
    >
    > One of the expressions shall have type "pointer to object _type_",
    > the other expression shall have integer type, and the result has
    > type "_type_".
    >
    > Since char is an integer type, it's perfectly legal to use a char as
    > an array index.
    >
    > It is suspicious, though. It's implementation-defined whether char is
    > signed or unsigned, and the value is promoted to int (or conceivably
    > to unsigned int) anyway. The space saving for a single declared
    > object is insignificant. There's almost never a good reason to use a
    > char as an array index.
    >


    It's hard to use char at all. Virtually any expression referring to char
    is converted immediately to int to fit a register or accumulator. There
    are no char-width places to put it.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Oct 13, 2005
    #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. FE
    Replies:
    0
    Views:
    1,656
  2. Alex Rast
    Replies:
    3
    Views:
    5,181
    Jim Lewis
    Oct 6, 2003
  3. Victor Hannak
    Replies:
    1
    Views:
    541
    Mike Treseler
    Nov 25, 2003
  4. Amal
    Replies:
    5
    Views:
    8,697
    Brandon
    Mar 8, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,134
    Philipp
    Jan 20, 2009
Loading...

Share This Page