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
307 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
7 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 43 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 41 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 41 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
If there a quick way to convert a cardinal value into 4, 8-bit bytes? 7 523
Buffer bomb nitro 1 4,285
port mapped I/o query 10 185
how to solve the error 1935: 1 758
How do we balance the user experience (UX) with reasonable security measures? It can be done, if you keep these fundamentals in mind.
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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