problem about allocating local variable

Discussion in 'C Programming' started by Prawit Chaivong, Jun 28, 2004.

  1. Hi All
    I don't know whether I should post this thing to compiler group or
    this group.
    Anyway, I decided to post to this group. (I'm sorry if you think I
    post in wrong group)

    My question is...
    I wrote the program like this.

    void func()
    {
    char a[8];
    }

    And then compile to asm code (gcc with -S option)
    I've got this line of code.

    ......
    subl $8 %esp
    ......

    That makes sense, It moves stack pointer down 8 bytes for local
    variable.
    But the problem is, when I change size of array to 9 or 7 the asm code
    turn
    to this.

    .....
    subl $24 %esp
    .....

    It doesn't make any sense to me. Why it allocates 24 bytes for 9 or 7
    bytes variable.

    Does anybody has any explaination?

    Thank in advance.
    Prawit C.
     
    Prawit Chaivong, Jun 28, 2004
    #1
    1. Advertising

  2. In 'comp.lang.c', (Prawit Chaivong) wrote:

    > void func()
    > {
    > char a[8];
    > }
    >
    > And then compile to asm code (gcc with -S option)
    > I've got this line of code.
    >
    > subl $8 %esp
    >
    > That makes sense, It moves stack pointer down 8 bytes for local
    > variable.
    > But the problem is, when I change size of array to 9 or 7 the asm code
    > turn
    > to this.


    > subl $24 %esp


    > It doesn't make any sense to me. Why it allocates 24 bytes for 9 or 7
    > bytes variable.


    It's probably due to some alignment constraint. It's not a C-language
    question, it's an implementation issue. Better to ask this on a newsgroup
    dedicated to your implementation of the C-language.

    news:gnu.gcc.help

    for a start.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jun 28, 2004
    #2
    1. Advertising

  3. (Prawit Chaivong) typa:

    >Hi All
    >I don't know whether I should post this thing to compiler group or
    >this group.
    >Anyway, I decided to post to this group. (I'm sorry if you think I
    >post in wrong group)
    >
    >My question is...
    >I wrote the program like this.
    >
    >void func()
    >{
    > char a[8];
    >}
    >
    >And then compile to asm code (gcc with -S option)
    >I've got this line of code.
    >
    >.....
    >subl $8 %esp
    >.....
    >
    >That makes sense, It moves stack pointer down 8 bytes for local
    >variable.
    >But the problem is, when I change size of array to 9 or 7 the asm code
    >turn
    >to this.
    >
    >....
    >subl $24 %esp
    >....

    Same think for me with asm output and CPU debug trace.

    On the same IDE, with Borland bc++5.6 and gcc 3.2, i tried :

    void func()
    {
    int i;
    char a[??];
    printf("%d\n", (int)&i - (int)&a[0]);
    }

    Results accords tou yours:

    ?? output bc++5.6 output gcc 3.2
    -------------------------------------------
    0 no compile 28
    1 1 1
    2 2 2
    3 3 28
    4 4 4
    5 8 28
    6 8 28
    7 8 28
    8 8 12
    9 12 28
    10 12 28
    11 12 28
    12 12 28
    13 16 28
    14 16 28
    15 16 28
    16 16 28
    17 20 44
    18 20 44

    gcc seems crazy (bug ?).

    bc++5.6 does not support VLAs. But wit gcc i tried:


    void func(int N)
    {
    int i;
    char a[N];
    printf("%d\n", (int)&i - (int)&a[0]);
    }

    int main(void)
    {
    for(int i = 0; i <= 32; i++)
    {
    printf("%d\t", i);
    func(i);
    }
    return 0;
    }

    The output is:

    0 16
    1 32
    2 32
    3 32
    4 32
    5 32
    6 32
    7 32
    8 32
    9 32
    10 32
    11 32
    12 32
    13 32
    14 32
    15 32
    16 32
    17 48
    18 48
    19 48
    20 48
    21 48
    22 48
    23 48
    24 48
    25 48
    26 48
    27 48
    28 48
    29 48
    30 48
    31 48
    32 48





    >It doesn't make any sense to me. Why it allocates 24 bytes for 9 or 7
    >bytes variable.
    >
    >Does anybody has any explaination?

    Not any sensible ....
    --
    Pierre
     
    Pierre Maurette, Jun 28, 2004
    #3
  4. Emmanuel Delahaye, Jun 28, 2004
    #4
  5. Emmanuel Delahaye <> typa:

    >In 'comp.lang.c', Pierre Maurette <> wrote:
    >
    >> Results accords tou yours:

    >
    >Results comply with yours:
    >
    >(well, I think)

    "tou" instead of "to" was a typo.
    For the rest, my english is even worse than my C style ;-)
    --
    Pierre
     
    Pierre Maurette, Jun 28, 2004
    #5
  6. Prawit Chaivong

    Richard Bos Guest

    Emmanuel Delahaye <> wrote:

    > In 'comp.lang.c', Pierre Maurette <> wrote:
    >
    > > Results accords tou yours:

    >
    > Results comply with yours:


    "Agree with" is, AFAICT (not being Anglophone myself either, after all)
    more idiomatically correct. "Comply with" would be used for rules, not
    for observations.

    Richard
     
    Richard Bos, Jun 28, 2004
    #6
  7. In 'comp.lang.c', (Richard Bos) wrote:

    > Emmanuel Delahaye <> wrote:
    >
    >> In 'comp.lang.c', Pierre Maurette <> wrote:
    >>
    >> > Results accords tou yours:

    >>
    >> Results comply with yours:

    >
    > "Agree with" is, AFAICT (not being Anglophone myself either, after all)
    > more idiomatically correct. "Comply with" would be used for rules, not
    > for observations.


    Agreed !

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jun 28, 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. Patrick Hoffmann
    Replies:
    3
    Views:
    2,911
    Christian Jan├čen
    Aug 8, 2003
  2. Replies:
    16
    Views:
    679
    Mark McIntyre
    Jun 26, 2006
  3. Raghu Kodali
    Replies:
    7
    Views:
    361
  4. Angus
    Replies:
    3
    Views:
    267
    Jim Langston
    Oct 10, 2006
  5. Rakesh Kumar
    Replies:
    5
    Views:
    694
    James Kanze
    Dec 21, 2007
Loading...

Share This Page