Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8513505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:35:23+00:00 2026-06-11T04:35:23+00:00

My question is related to Stack allocation, padding, and alignment . Consider the following

  • 0

My question is related to Stack allocation, padding, and alignment. Consider the following function:

void func(int a,int b)
{
    char buffer[5];
}

At assembly level, the function looks like this:

pushl   %ebp
movl    %esp, %ebp
subl    $24, %esp

I want to know how the 24 bytes on the stack is allocated. I understand that 16 bytes is allocated for the char buffer[5]. I don’t understand why the extra 8 bytes are for and how they are allocated. The top answer in the above link says that it is for ret and leave. Can someone please expand on that?

I’m thinking that the stack structure looks like this:

[bottom] b , a , return address , frame pointer , buffer1 [top]

But this could be wrong because i’m writing a simple buffer overflow and trying to change the return address. But for some reason the return address is not changing. Is something else present on the stack?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:35:25+00:00Added an answer on June 11, 2026 at 4:35 am

    There are a couple of reasons for extra space. One is for alignment of variables. A second is to introduce padding for checking the stack (typically a debug build rather than a release build use of space). A third is to have additional space for temporary storage of registers or compiler generated temporary variables.

    In the C calling sequence, the way it is normally done is there will be a series of push instructions pushing the arguments onto the stack and then a call instruction is used to call the function. The call instruction will push the return address onto the stack.

    When the function returns, the calling function will then remove the pushed on arguments. For instance the call to a function (this is Visual Studio 2005 with a C++ program) will look like:

    push    OFFSET ?pHead@@3VPerson@@A      ; pHead
    call    ?exterminateStartingFrom@@YAXPAVPerson@@@Z ; exterminateStartingFrom
    add esp, 4
    

    This is pushing the address of a variable onto the stack, calling the function (the function name is mangled per C++), and then after the called function returns, it readjusts the stack by adding to the stack pointer the number of bytes used for the address.

    The following is the entry part of the called function. What this does is to allocate space on the stack for the local variables. Notice that after setting up the entry environment, it then gets the function argument from the stack.

    push    ebp
    mov ebp, esp
    sub esp, 232                ; 000000e8H
    push    ebx
    push    esi
    push    edi
    lea edi, DWORD PTR [ebp-232]
    

    When the function returns, it basically adjusts the stack back to where it was at the time the function was called. Each function is responsible for cleaning up whatever changes it has made to the stack before it returns.

    pop edi
    pop esi
    pop ebx
    add esp, 232                ; 000000e8H
    pop ebp
    ret 0
    

    You mention that you are trying to change the return address. From these examples what you can see is that the return address is after the last argument that was pushed onto the stack.

    Here is a brief writeup on function call conventions. Also take a look at this document on Intel assembler instructions.

    Doing some example work with Visual Studio 2005 what I see is that if I do the following code, I can access the return for this example function.

    void MyFunct (unsigned short arg) {
        unsigned char  *retAddress = (unsigned char *)&arg;
        retAddress -=4;
        printf ("Return address is 0x%2.2x%2.2x%2.2x%2.2x\n", retAddress[3], retAddress[2], retAddress[1], retAddress[0]);
    }
    

    Notice that the call assembler instruction for this Windows 32 bit addressing appears to put the return address in a byte order in which the return address is stored from low byte to high byte.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Preface: This is my first audio-related question on Stack Overflow, so I'll try
I've looked at other questions on Stack Overflow related to this question, but none
I want to start learning Forth (like in the related Stack Overflow question Is
Background This question is related to Why does String.valueOf(null) throw a NullPointerException? Consider the
Stack Overflow offers related questions (on the right side of this question). I have
Related to this Stack Overflow question (C state-machine design) , could you Stack Overflow folks share
This is closely related to another question: How can I measure thread stack depth?
Kind of related to this question: https://stackoverflow.com/questions/8708525/how-to-check-if-mako-function-exist I want to check if a function
I know there are a thousand data import related questions on Stack Overflow, and
Question related to PHP memory-handling from someone not yet very experienced in PHP: If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.