problem about allocating local variable

P

Prawit Chaivong

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.
 
E

Emmanuel Delahaye

In said:
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.



for a start.
 
P

Pierre Maurette

(e-mail address 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 ....
 
P

Pierre Maurette

Emmanuel Delahaye said:
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 ;-)
 
R

Richard Bos

Emmanuel Delahaye said:
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
 
E

Emmanuel Delahaye

In said:
"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 !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top