473,498 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory allocation of local variables

Hi all,
I tried compiling the above two programs :
on x86, 32 bit machines.

And when I used objdump on that I saw the following code.
Can anyone help me know,
Why in the objdump of our first program the esp is decremented by 18H
bytes
and in the second program the esp is decremented by 28H bytes.

How actually is teh memory allocated by gcc for local variables.

Kindly help.

int main()
{
char x;
return 0;

}
[root@localhost ~]# gcc test.c
[root@localhost ~]# objdump -S a.out | less
08048348 <main>:
8048348: 55 push %ebp
8048349: 89 e5 mov %esp,%ebp
804834b: 83 ec 18 sub $0x18,%esp
804834e: 83 e4 f0 and $0xfffffff0,%esp
8048351: b8 00 00 00 00 mov $0x0,%eax
8048356: 83 c0 0f add $0xf,%eax
8048359: 83 c0 0f add $0xf,%eax
804835c: c1 e8 04 shr $0x4,%eax
804835f: c1 e0 04 shl $0x4,%eax
8048362: 29 c4 sub %eax,%esp
8048364: b8 00 00 00 00 mov $0x0,%eax
8048369: c9 leave
804836a: c3 ret
804836b: 90 nop

int main()
{
double x,y,z;
char p,q,r;
return 0;

}

08048348 <main>:
8048348: 55 push %ebp
8048349: 89 e5 mov %esp,%ebp
804834b: 83 ec 28 sub $0x28,%esp
804834e: 83 e4 f0 and $0xfffffff0,%esp
8048351: b8 00 00 00 00 mov $0x0,%eax
8048356: 83 c0 0f add $0xf,%eax
8048359: 83 c0 0f add $0xf,%eax
804835c: c1 e8 04 shr $0x4,%eax
804835f: c1 e0 04 shl $0x4,%eax
8048362: 29 c4 sub %eax,%esp
8048364: b8 00 00 00 00 mov $0x0,%eax
8048369: c9 leave
804836a: c3 ret
804836b: 90 nop


--
Regards,
Sandeep

Dec 13 '06 #1
2 2309
In article <11**********************@j72g2000cwa.googlegroups .com>,
sandy <sa***********@gmail.comwrote:
>Why in the objdump of our first program the esp is decremented by 18H
bytes
and in the second program the esp is decremented by 28H bytes.

How actually is teh memory allocated by gcc for local variables.
For details of gcc, you should try a gcc-related group.

But you might find it enlightening to print out the addresses of the
variables (using the "%p" format in printf()). I didn't.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 13 '06 #2
"sandy" <sa***********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
I tried compiling the above two programs :
on x86, 32 bit machines.

And when I used objdump on that I saw the following code.
Can anyone help me know,
Why in the objdump of our first program the esp is decremented by
18H bytes and in the second program the esp is decremented by
28H bytes.

How actually is teh memory allocated by gcc for local variables.
How any particular implementation accomplishes that is off-topic here;
all comp.lang.c cares about is that the Standard guarantees that it
happens somehow -- and that's all you need to know to write portable
code. If you start digging into what happens under the hood, you're
going to end up writing non-portable code.

<OT>In this particular case, GCC is setting up the stack frame to hold a
certain number of bytes of local variables, including padding. If you
don't know what a stack frame is, or don't know why modifying ESP
affects it, you're probably better off asking in comp.lang.asm.x86.</OT>

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Dec 13 '06 #3

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

Similar topics

3
3165
by: Anoop | last post by:
Hi guys i have a piece of code main() { switch(...) { case 1: {
9
2000
by: Rafael Charnovscki | last post by:
I can comprehend the basics of that subject (pointers, memory allocation, heap, stack etc), but I am interested to have references with more details. I have some C/C++ books and lots of URLs...
10
14021
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
3
1634
by: Richard | last post by:
I need to dynamically allocation memory at run time for the number of student's records and their test scores for the program code below. I don't understand what the 3 errors i got. I can anyone...
8
1987
by: Cuthbert | last post by:
Hi folks, This question is a little deep into memory management of microprocessor. How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top? For example, the...
18
2607
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
53
26321
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
1
1708
by: krishna81m | last post by:
In the following code, I am trying to return a char, a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at...
14
3809
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
5
1555
by: peridian | last post by:
Hello, I may be remembering incorrectly, but I'm sure that in C++ you could declare two variables of the same name where the second declaration is within it's own code block. E.g. void...
0
7125
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,...
0
7203
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...
0
7379
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...
1
4908
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...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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...

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.