473,756 Members | 4,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem about allocating local variable

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.
Nov 14 '05 #1
6 1351
In 'comp.lang.c', ar*********@hot mail.com (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.he lp

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/
Nov 14 '05 #2
ar*********@hot mail.com (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
Nov 14 '05 #3
In 'comp.lang.c', Pierre Maurette <ma************ *@free.fr> 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/
Nov 14 '05 #4
Emmanuel Delahaye <em**********@n oos.fr> typa:
In 'comp.lang.c', Pierre Maurette <ma************ *@free.fr> 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
Nov 14 '05 #5
Emmanuel Delahaye <em**********@n oos.fr> wrote:
In 'comp.lang.c', Pierre Maurette <ma************ *@free.fr> 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
Nov 14 '05 #6
In 'comp.lang.c', rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Emmanuel Delahaye <em**********@n oos.fr> wrote:
In 'comp.lang.c', Pierre Maurette <ma************ *@free.fr> 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/
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1860
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little allocations (approx. 16-512 bytes). Is my feeling about this justified? thanks! --
5
1776
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way of the text, the program is supposed to demonstrate the use of a copy constructor function for the object created on the heap (because there is a char* pointer variable in the instantiated class) and how you would use the delete keyword with the...
3
1840
by: Milan Gornik | last post by:
Hello to all, My question is on right way of returning newly created class from a function (and thus, from class method or operator). As I currently see it, there are two different ways to do that. One way is to create new class as a local (automatic) variable in function and then to return it:
23
2411
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file, there are lots of errors and warnings in compiling stage. I am really confused about this problem because I'm not even familiar with C++ and my code only includes simple C functions. Can anybody please tell me what's my mistake? Is there any other...
16
1566
by: junky_fellow | last post by:
Hi guys, Consider the following piece of code: func() { if(x==0) { int arr; /* do something */
15
4792
by: syang8 | last post by:
hi, folks, I use Kdevelop to build some scientific simulation on Linux. If I set the size of an array N = 8000, the program works fine. However, if I set the array N some number greater than 10000 (actually, what I need is 80000), the program has segmentation error. The intersting thing is that the positions reporting segmentation error are different if I set N to be different values. What problem is this usually? I guess must be...
5
2284
by: vfunc | last post by:
I have a function "test" in a seperate file to main and a segmentation fault seems to occur before the first line of the test function is executed. void test() { DEBUG_LOG("in test"); maca c; .... }
3
1912
by: Angus | last post by:
Hello I have a member variable: std::map<int, CAgentsm_AgentsList; CAgents is just a really small class with some member variables. Just really a container for agent data. Agents log in to a server. In my login function I do this: CAgents myagent;
11
12467
by: Krzysztof Retel | last post by:
Hi guys, I am struggling writing fast UDP server. It has to handle around 10000 UDP packets per second. I started building that with non blocking socket and threads. Unfortunately my approach does not work at all. I wrote a simple case test: client and server. The client sends 2200 packets within 0.137447118759 secs. The tcpdump received 2189 packets, which is not bad at all. But the server only handles 700 -- 870 packets, when it is...
0
9462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.