Another silly question: array with vars

Discussion in 'C Programming' started by Sensei, Jan 22, 2007.

  1. Sensei

    Sensei Guest

    Hi again! I have ``yet another silly question'', about arrays this time.

    I've looked through the FAQs in the array & memory sections without
    finding an answer. I surely didn't look deep enough.

    What does the standards (plural! C99 and pre-C99 standards) say about
    variable-initialized arrays like in the following code?

    int main(void)
    {
    int a = 6;
    char s[a];
    /* ... */
    return 0;
    }

    If my memory doesn't fail, that is not pre-C99 compliant and resulting
    in a compiler error, but I'm not sure... And does C99 state something
    about the initializer ``int a''? I mean, what if it is NOT initialized?

    I know, curiosity killed the cat :)

    --
    Sensei <senseiwa@Apple's mail>

    Research (n.): a discovery already published by a chinese guy one month
    before you, copying a russian who did it in the 60s.
    Sensei, Jan 22, 2007
    #1
    1. Advertising

  2. Sensei

    Mike Wahler Guest

    "Sensei" <senseiwa@apple's mail.com> wrote in message
    news:45b4734c$0$30016$...
    > Hi again! I have ``yet another silly question'', about arrays this time.
    >
    > I've looked through the FAQs in the array & memory sections without
    > finding an answer. I surely didn't look deep enough.
    >
    > What does the standards (plural! C99 and pre-C99 standards) say about
    > variable-initialized arrays like in the following code?
    >
    > int main(void)
    > {
    > int a = 6;
    > char s[a];
    > /* ... */
    > return 0;
    > }
    >
    > If my memory doesn't fail, that is not pre-C99 compliant and resulting in
    > a compiler error, but I'm not sure...


    That's correct. The array's size must be specified
    by a constant expression. In the code above, 'a' is not.

    > And does C99 state something about the initializer ``int a''?


    'int a' is not an initializer, it's a declaration (as well
    as a definition).

    >I mean, what if it is NOT initialized?


    If an object is not initialized or assigned a valid value, evaluating
    its value in any context produces undefined behavior.

    -Mike
    Mike Wahler, Jan 22, 2007
    #2
    1. Advertising

  3. Mike Wahler wrote:
    > "Sensei" <senseiwa@apple's mail.com> wrote in message
    > news:45b4734c$0$30016$...
    > >I mean, what if it is NOT initialized?

    >
    > If an object is not initialized or assigned a valid value, evaluating
    > its value in any context produces undefined behavior.


    Actually, C99 changed the rules on that. The value of an uninitialised
    object is indeterminate. An indeterminate value may be either an
    unspecified but valid value, or a trap representation. If the type has
    no trap representations, there is no possible undefined behaviour in
    reading the object.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jan 22, 2007
    #3
  4. Sensei <senseiwa@apple's mail.com> writes:
    [...]
    > What does the standards (plural! C99 and pre-C99 standards) say about
    > variable-initialized arrays like in the following code?
    >
    > int main(void)
    > {
    > int a = 6;
    > char s[a];
    > /* ... */
    > return 0;
    > }
    >
    > If my memory doesn't fail, that is not pre-C99 compliant and resulting
    > in a compiler error, but I'm not sure... And does C99 state something
    > about the initializer ``int a''? I mean, what if it is NOT initialized?


    s is a variable *length* array, not a variable initialized array; it's
    also referred to as a VLA.

    VLAs were introduced in C99. C90 does not support them.

    If "a" were not initialized, any attempt to access its value,
    including using it in a VLA declaration, would invoke undefined
    behavior.

    --
    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, Jan 22, 2007
    #4
  5. Keith Thompson <> writes:
    > Sensei <senseiwa@apple's mail.com> writes:
    > [...]
    > > What does the standards (plural! C99 and pre-C99 standards) say about
    > > variable-initialized arrays like in the following code?
    > >
    > > int main(void)
    > > {
    > > int a = 6;
    > > char s[a];
    > > /* ... */
    > > return 0;
    > > }
    > >
    > > If my memory doesn't fail, that is not pre-C99 compliant and resulting
    > > in a compiler error, but I'm not sure... And does C99 state something
    > > about the initializer ``int a''? I mean, what if it is NOT initialized?

    >
    > s is a variable *length* array, not a variable initialized array; it's
    > also referred to as a VLA.
    >
    > VLAs were introduced in C99. C90 does not support them.
    >
    > If "a" were not initialized, any attempt to access its value,
    > including using it in a VLA declaration, would invoke undefined
    > behavior.


    As Harald points out, accessing an int object probably doesn't invoke
    UB if int has no trap representations (and it's possible for a program
    to prove that it doesn't by looking at CHAR_BIT*sizeof(int), INT_MIN,
    and INT_MAX). But it's still a Very Bad Idea. In this case, it's
    likely to result in either undefined behavior if "a" happens to be
    negative, or stack overflow^H^H^H^H^H^H^H^H^H^H^H^H^H^H an attempt to
    create an object whose size exceeds implementation limits (and, again,
    undefined behavior) if the value of "a" happens to be large.

    And let me issue a plea to avoid the use of "a" as an identifier in
    sample code, or of any other identifier that can easily be confused
    with an English word. "x" and "s" are ok.

    --
    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, Jan 22, 2007
    #5
  6. Sensei

    Sensei Guest

    On 2007-01-22 10:14:31 +0100, "Harald van Dijk" <> said:

    > Mike Wahler wrote:
    >> "Sensei" <senseiwa@apple's mail.com> wrote in message
    >> news:45b4734c$0$30016$...
    >>> I mean, what if it is NOT initialized?

    >>
    >> If an object is not initialized or assigned a valid value, evaluating
    >> its value in any context produces undefined behavior.

    >
    > Actually, C99 changed the rules on that. The value of an uninitialised
    > object is indeterminate. An indeterminate value may be either an
    > unspecified but valid value, or a trap representation. If the type has
    > no trap representations, there is no possible undefined behaviour in
    > reading the object.


    Then

    int i;
    int array;

    does not invoke an UB, but does C99 say anything about the actual
    storage? From some trials I'd say that compilers usually allocate a
    certain amount of memory, but I don't know if this is against the
    standard or not.

    --
    Sensei <senseiwa@Apple's mail>

    Research (n.): a discovery already published by a chinese guy one month
    before you, copying a russian who did it in the 60s.
    Sensei, Jan 23, 2007
    #6
  7. Sensei

    Sensei Guest

    On 2007-01-22 23:09:42 +0100, Keith Thompson <> said:

    >> s is a variable *length* array, not a variable initialized array; it's
    >> also referred to as a VLA.
    >>
    >> VLAs were introduced in C99. C90 does not support them.
    >>
    >> If "a" were not initialized, any attempt to access its value,
    >> including using it in a VLA declaration, would invoke undefined
    >> behavior.

    >
    > As Harald points out, accessing an int object probably doesn't invoke
    > UB if int has no trap representations (and it's possible for a program
    > to prove that it doesn't by looking at CHAR_BIT*sizeof(int), INT_MIN,
    > and INT_MAX). But it's still a Very Bad Idea. In this case, it's
    > likely to result in either undefined behavior if "a" happens to be
    > negative, or stack overflow^H^H^H^H^H^H^H^H^H^H^H^H^H^H an attempt to
    > create an object whose size exceeds implementation limits (and, again,
    > undefined behavior) if the value of "a" happens to be large.


    One more thing, weren't simple types as int, char, and so on
    initialized to 0 with C99? I remember reading this somewhere, and of
    course I might be completely wrong.

    > And let me issue a plea to avoid the use of "a" as an identifier in
    > sample code, or of any other identifier that can easily be confused
    > with an English word. "x" and "s" are ok.


    Ok, I'll do that :)

    --
    Sensei <senseiwa@Apple's mail>

    Research (n.): a discovery already published by a chinese guy one month
    before you, copying a russian who did it in the 60s.
    Sensei, Jan 23, 2007
    #7
  8. Sensei

    Ian Collins Guest

    Sensei wrote:
    >
    > One more thing, weren't simple types as int, char, and so on initialized
    > to 0 with C99? I remember reading this somewhere, and of course I might
    > be completely wrong.
    >

    Not for automatic objects, the value is indeterminate. Maybe you where
    thinking of compound initialisers?

    --
    Ian Collins.
    Ian Collins, Jan 23, 2007
    #8
  9. Sensei

    Ian Collins Guest

    Sensei wrote:
    > On 2007-01-22 10:14:31 +0100, "Harald van Dijk" <> said:
    >
    >> Mike Wahler wrote:
    >>
    >>> "Sensei" <senseiwa@apple's mail.com> wrote in message
    >>> news:45b4734c$0$30016$...
    >>>
    >>>> I mean, what if it is NOT initialized?
    >>>
    >>>
    >>> If an object is not initialized or assigned a valid value, evaluating
    >>> its value in any context produces undefined behavior.

    >>
    >>
    >> Actually, C99 changed the rules on that. The value of an uninitialised
    >> object is indeterminate. An indeterminate value may be either an
    >> unspecified but valid value, or a trap representation. If the type has
    >> no trap representations, there is no possible undefined behaviour in
    >> reading the object.

    >
    >
    > Then
    >
    > int i;
    > int array;
    >
    > does not invoke an UB, but does C99 say anything about the actual
    > storage? From some trials I'd say that compilers usually allocate a
    > certain amount of memory, but I don't know if this is against the
    > standard or not.
    >

    Well considering i can hold any value, it's pot luck how big a array you
    get and due to the lack of error reporting with VLAs, attempting to use
    the array may do odd things.

    --
    Ian Collins.
    Ian Collins, Jan 23, 2007
    #9
  10. Sensei

    Sensei Guest

    On 2007-01-23 09:27:23 +0100, Ian Collins <> said:

    > Sensei wrote:
    >>
    >> One more thing, weren't simple types as int, char, and so on initialized
    >> to 0 with C99? I remember reading this somewhere, and of course I might
    >> be completely wrong.
    >>

    > Not for automatic objects, the value is indeterminate. Maybe you where
    > thinking of compound initialisers?


    Probably I'm just confusing the two. Thanks, you've been really clear!

    --
    Sensei <senseiwa@Apple's mail>

    Research (n.): a discovery already published by a chinese guy one month
    before you, copying a russian who did it in the 60s.
    Sensei, Jan 23, 2007
    #10
  11. Sensei

    HyperCube Guest

    On Jan 22, 4:20 pm, Sensei <senseiwa@apple's mail.com> wrote:
    > Hi again! I have ``yet another silly question'', about arrays this time.
    >
    > I've looked through the FAQs in the array & memory sections without
    > finding an answer. I surely didn't look deep enough.
    >
    > What does the standards (plural! C99 and pre-C99 standards) say about
    > variable-initialized arrays like in the following code?
    >
    > int main(void)
    > {
    > int a = 6;
    > char s[a];
    > /* ... */
    > return 0;
    >
    > }If my memory doesn't fail, that is not pre-C99 compliant and resulting
    > in a compiler error, but I'm not sure... And does C99 state something
    > about the initializer ``int a''? I mean, what if it is NOT initialized?
    >
    > I know, curiosity killed the cat :)
    >
    > --
    > Sensei <senseiwa@Apple's mail>
    >
    > Research (n.): a discovery already published by a chinese guy one month
    > before you, copying a russian who did it in the 60s.



    i think (... i'm sure) you HVAE TO use a 'constant' to specify the size
    of a array, because the memory space of the array will be allocate
    before the program start to run, and at that time, the value of your
    variable 'a' is not known yet.
    HyperCube, Jan 24, 2007
    #11
  12. "HyperCube" <> writes:
    > On Jan 22, 4:20 pm, Sensei <senseiwa@apple's mail.com> wrote:
    > > Hi again! I have ``yet another silly question'', about arrays this time.
    > >
    > > I've looked through the FAQs in the array & memory sections without
    > > finding an answer. I surely didn't look deep enough.
    > >
    > > What does the standards (plural! C99 and pre-C99 standards) say about
    > > variable-initialized arrays like in the following code?
    > >
    > > int main(void)
    > > {
    > > int a = 6;
    > > char s[a];
    > > /* ... */
    > > return 0;
    > >
    > > }If my memory doesn't fail, that is not pre-C99 compliant and resulting
    > > in a compiler error, but I'm not sure... And does C99 state something
    > > about the initializer ``int a''? I mean, what if it is NOT initialized?

    [...]
    >
    > i think (... i'm sure) you HVAE TO use a 'constant' to specify the size
    > of a array, because the memory space of the array will be allocate
    > before the program start to run, and at that time, the value of your
    > variable 'a' is not known yet.


    Nope.

    The array "s" is inside a function, so memory for it is allocated when
    the function is called, not on program startup. The distinction is a
    subtle one for main(), but it still aplies.

    C99 supports variable-length arrays (VLAs).

    This has been mentioned in this thread.

    --
    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, Jan 24, 2007
    #12
    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. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    392
  2. Linuxguy123
    Replies:
    7
    Views:
    685
    Paddy O'Loughlin
    Feb 20, 2009
  3. caccolangrifata
    Replies:
    18
    Views:
    393
    Chris Torek
    Jul 22, 2011
  4. Cere Davis

    another silly cgi question

    Cere Davis, Feb 19, 2004, in forum: Ruby
    Replies:
    2
    Views:
    151
    Cere Davis
    Feb 20, 2004
  5. Mike Keller

    Silly Array Question

    Mike Keller, Jan 22, 2007, in forum: Ruby
    Replies:
    11
    Views:
    146
    Alex Young
    Jan 23, 2007
Loading...

Share This Page