Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Number of bytes allocated for char arrays by subl %esp seems much too high (in linux 2.4, gcc 3.2.2)

Posted on 2003-12-06
7
Medium Priority
?
347 Views
Last Modified: 2008-03-17
Hi!

I noticed that when I declare a buffer of, for example, char[5], the assembly code generated by gcc actually allocates a much larger buffer, in this case 24 bytes:

subl $24, (%esp)

How can this be explained?
0
Comment
Question by:dn6
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 172 total points
ID: 9888440
If you mean at function entry, the compiler might allocate some extra space to store temporary results, or to store registers, or to align the stack pointer, or to set up some kind of exception stack frame, or to pre-allocate some space needed in any variables declared in interior code blocks.  
0
 
LVL 37

Assisted Solution

by:bbao
bbao earned 164 total points
ID: 9893387
for static or constant char buffer, commonly the compiler may allocate same-size memory, or a bit more for memory alignment. for char buffer of a function, depends on its type, usage and how compiler push them into stack. for dynamic allocated char buffer, it needs extra memory for MCB, memory control block, it describes the size, type and other attributes of a memory block.

hope it helps,
bbao
0
 
LVL 5

Assisted Solution

by:mtmike
mtmike earned 164 total points
ID: 9897137
GCC aligns the stack pointer to a multiple of 16 bytes by default. You can change its behaviour using the -mpreferred-stack-boundary switch. For example, 'gcc -mpreferred-stack-boundary=2' forces gcc to align the stack pointer to a multiple of 2^2=4 bytes. Optimizing for size (-Os) has the same effect.
0
 
LVL 37

Expert Comment

by:bbao
ID: 9911073
yes, it depends on type, processor, operating system and even compiler.
0
 
LVL 37

Expert Comment

by:bbao
ID: 10054045
dn6, any feedback please?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question