Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem about allocating local variable

Reply
Thread Tools

problem about allocating local variable

 
 
Prawit Chaivong
Guest
Posts: n/a
 
      06-28-2004
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.
 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-28-2004
In 'comp.lang.c', http://www.velocityreviews.com/forums/(E-Mail Removed) (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/
 
Reply With Quote
 
 
 
 
Pierre Maurette
Guest
Posts: n/a
 
      06-28-2004
(E-Mail Removed) (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
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-28-2004
In 'comp.lang.c', Pierre Maurette <(E-Mail Removed)> wrote:

> Results accords tou yours:


Results comply with yours:

(well, I think)

--
-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/
 
Reply With Quote
 
Pierre Maurette
Guest
Posts: n/a
 
      06-28-2004
Emmanuel Delahaye <(E-Mail Removed)> typa:

>In 'comp.lang.c', Pierre Maurette <(E-Mail Removed)> 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
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-28-2004
Emmanuel Delahaye <(E-Mail Removed)> wrote:

> In 'comp.lang.c', Pierre Maurette <(E-Mail Removed)> 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
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-28-2004
In 'comp.lang.c', (E-Mail Removed) (Richard Bos) wrote:

> Emmanuel Delahaye <(E-Mail Removed)> wrote:
>
>> In 'comp.lang.c', Pierre Maurette <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
Allocating a local variable to a member function Angus C++ 3 10-10-2006 08:50 PM
allocating space for local variables junky_fellow@yahoo.co.in C Programming 16 06-26-2006 10:05 PM
Passing an evaluated variable to new in allocating a derived class Raghu Kodali C++ 7 01-18-2006 06:02 PM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments