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

The Archive Base Latest Questions

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

Take this structure for example: struct Packing { int x; // 4-byte align int

  • 0

Take this structure for example:

struct Packing
{
     int x; // 4-byte align
     int y; // 4-byte align
     short int z; // 2-byte align
     char m; // 1-byte align;
     char _pad[1]; // explicit padding
};

The sizeof this structure is 12-bytes.

So should store this struct in addresses multiples of the struct size (12-bytes) or in multiples of sizeof(int) (the largest alignment requirement among the members of the struct)?

Since multiples of 12 are also multiples of 4 (sizeof(int)) I guess the struct will be correctly aligned in addresses multiples of 12, but I might waste space that wouldnt be wasted if it was 4-byte aligned.

EDIT: At address 0x00000012 the structure would be aligned and the its first member would also be aligned because 12 is a multiple of 4.
What if stored it at address 0x00000004? In this case the first element of the struct would be aligned but what about the structure itself?

  • 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-25T02:31:26+00:00Added an answer on May 25, 2026 at 2:31 am

    The optimal alignment for a struct is equal to the largest alignment for any of the struct’s members. In this case that is 4.

    Update

    The above assumes that the primary operation you perform on the struct is accessing its members. See the comments to Necrolis’s answer for more discussion. In short I suspect that the real answer to your question depends strongly on the hardware involved and the algorithms you are using.

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

Sidebar

Related Questions

Take this class as example: public class Category : PersistentObject<int> { public virtual string
Take this file as example,there are many non-extern structures like: struct list_head source_list; How
Take this example code (ignore it being horribly inefficient for the moment) let listToString
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
So, take this html for example: <ul> <li> <a href=#>Test</a> <ul> <li><a href=#>Test</a></li> <li><a
So i'm having trouble figuring how to overcome this. Take for example, i have
I have this DB Structure: Service Id int PK CategoryId FK SomeOtherFields LocalizedService LocalizedServiceId
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string

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.