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 6772287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:31:55+00:00 2026-05-26T15:31:55+00:00

I am learning about how compilers represent C++ programs in assembly. I have a

  • 0

I am learning about how compilers represent C++ programs in assembly. I have a question about something that the compiler does that I can’t make sense of. Here is some C++ code:

class Class1 {
public:
  int i;
  char ch;
};

int main() {
  Class1 cls;
}

Compiling with “g++ -S ” outputs this (I’ve stripped out everything but the function definition):

main:
    push    ebp
    mov     ebp, esp
    sub     esp, 16
    mov     eax, 0
    leave
    ret

I don’t understand the line sub esp, 16. Why would it allocate 16 bytes for an instance of this class that only requires 8 when you take into account data structure alignment and padding?

It should be

[int i - 4 bytes][char ch - 1 byte][padding - 3 bytes]

should it not?

When I compiled the code with the class definition also including a double, i.e.

class Class1 {
public:
  int i;
  char ch;
  double dub;
}; 

it still allocated 16 bytes, which made sense in that case.

So why does the compiler allocate 16 bytes when it only needs 8?

  • 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-05-26T15:31:56+00:00Added an answer on May 26, 2026 at 3:31 pm

    This has to do with stack-frame alignment, not structure alignment.

    If you did a sizeof() on your objects, you’ll see what you expect with struct alignment and padding.

    However, stack-frames are slightly different. On most systems today, the stack alignment is 16 bytes (or more) to accommodate SSE memory accesses.

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

Sidebar

Related Questions

There is a question on stackoverflow about Learning to write a compiler . I
I have been learning about the basics of C# but haven't come across a
I am just learning about app.config in respect of creating custom sections. I have
I want to output D:\Learning\CS\Resource\Tutorial\C#LangTutorial But can't work. Compiler error error CS0165: Use of
I am learning C using gcc on fedora 16. I have several questions about
I am learning about Context Free Grammars for a Compilers course I'm attending to.
I'm learning about strings in c. I am using code::blocks as a compiler, even
Learning about ARM NEON intrinsics, I was timing a function that I wrote to
I'm learning more about compilers. While trying to understand this Wikipedia article on LLVM
I have been learning about move constructors over the last day or so, trying

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.