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

  • SEARCH
  • Home
  • 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 7862515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:09:23+00:00 2026-06-02T23:09:23+00:00

I understand the padding that takes place between the members of a struct to

  • 0

I understand the padding that takes place between the members of a struct to ensure correct alignment of individual types. However, why does the data structure have to be a multiple of alignment of largest member? I don’t understand the padding is needed at the end.

Reference:
http://en.wikipedia.org/wiki/Data_structure_alignment

  • 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-02T23:09:24+00:00Added an answer on June 2, 2026 at 11:09 pm

    Good question. Consider this hypothetical type:

    struct A {
        int n;
        bool flag;
    };
    

    So, an object of type A should take five bytes (four for the int plus one for the bool), but in fact it takes eight. Why?

    The answer is seen if you use the type like this:

    const size_t N = 100;
    A a[N];
    

    If each A were only five bytes, then a[0] would align but a[1], a[2] and most of the other elements would not.

    But why does alignment even matter? There are several reasons, all hardware-related. One reason is that recently/frequently used memory is cached in cache lines on the CPU silicon for rapid access. An aligned object smaller than a cache line always fits in a single line (but see the interesting comments appended below), but an unaligned object may straddle two lines, wasting cache.

    There are actually even more fundamental hardware reasons, having to do with the way byte-addressable data is transferred down a 32- or 64-bit data bus, quite apart from cache lines. Not only will misalignment clog the bus with extra fetches (due as before to straddling), but it will also force registers to shift bytes as they come in. Even worse, misalignment tends to confuse optimization logic (at least, Intel’s optimization manual says that it does, though I have no personal knowledge of this last point). So, misalignment is very bad from a performance standpoint.

    It usually is worth it to waste the padding bytes for these reasons.

    Update: The comments below are all useful. I recommend them.

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

Sidebar

Related Questions

As I understand, width/padding/margin properties only work on block level elements. However, on INPUT
I understand that: '\n' // literally the backslash character followed by the character for
I understand, from MSDN, that ClassInitialize is to mark a method that will do
I understand that JVM and CLR were designed as stack-based virtual machines. When JIT
I understand how to calculate margin-left / padding-left and margin-right / padding-right in a
Once again, I'm questioning a longstanding belief. Until today, I believed that the alignment
I dont understand how to create two date columns and create range between them?
I understand that different browsers are likely to have different ceiling values, but what
  I understand that elements can have multiple classes: .rfrsh-btn { background-image:url(../../upload/rfrsh_nb_grey.png); ... }
My question is related to Stack allocation, padding, and alignment . Consider the following

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.