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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:07:32+00:00 2026-05-27T14:07:32+00:00

This is exert from a book about data alignment of primitive types in memory.

  • 0

This is exert from a book about data alignment of primitive types in memory.

Microsoft Windows imposes a stronger alignment requirement—any primitive object of K bytes, for
K = 2, 4, or 8, must have an address that is a multiple of K. In particular, it requires that the address
of a double or a long long be a multiple of 8. This requirement enhances the memory performance at
the expense of some wasted space. The Linux convention, where 8-byte values are aligned on 4-byte
boundaries was probably good for the i386, back when memory was scarce and memory interfaces were
only 4 bytes wide. With modern processors, Microsoft’s alignment is a better design decision. Data type
long double, for which gcc generates IA32 code allocating 12 bytes (even though the actual data type
requires only 10 bytes) has a 4-byte alignment requirement with both Windows and Linux.

Questions are:

  1. What imposes data alignment, OS or compiler?
  2. Can I change it or it is fixed?
  • 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-27T14:07:33+00:00Added an answer on May 27, 2026 at 2:07 pm

    Generally speaking, it’s the compiler that imposes the alignment. Whenever you declare a primitive type (eg. double), the compiler will automatically align it to 8 bytes on the stack.

    Furthermore, memory allocations are also generally aligned to the largest primitive type so that you can safely do this:

    double *ptr = (double*)malloc(size);
    

    without having to worry about alignment.

    Therefore, generally speaking, if you’re programming with good habits, you won’t have to worry about alignment. One way to get something misaligned is to do something like this:

    char *ch_ptr = (char*)malloc(size);
    
    double *d_ptr = (double*)(ch_ptr + 1);
    

    There are some exceptions to this: When you start getting into SSE and vectorization, things get a bit messy because malloc no longer guarantees 16-byte alignment.


    To override the alignment of something, MSVC has the declspec(align) modifier which will allow this. It’s used to increase the alignment of something. Though I’m not sure if it lets you decrease the alignment of a primitive type. It says explicitly that you cannot decrease alignment with this modifier.


    EDIT :

    I found the documentation stating the alignment of malloc() on GCC:

    The address of a block returned by malloc or realloc in the GNU system
    is always a multiple of eight (or sixteen on 64-bit systems).

    Source: http://www.gnu.org/s/hello/manual/libc/Aligned-Memory-Blocks.html

    So yes, GCC now aligns to at least 8 bytes.

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

Sidebar

Related Questions

Hi guys so this is an exert from the code I have public ItemList()
So I'm far from an expert on C, but something's been bugging me about
I get this code example from Expert F# by Don Syme from chapter 13
This is re-posted from something I posted on the DDD Yahoo! group. All things
This is from Expert F# 2.0 page 231. Apparently the following block of code
Ok I've seen this question Entity Framework: Generate Database From Model removes Stored Procedures
firstly, let me quote a bit an essay from Expert Python Programming book: In
sed -e 's/ *-\{0,1\},\{0,1\} *[Pp][Aa]\{0,1\}[Rr]\{0,1\}[Tt].\{0,1\} *\([0-9]\{1,\}\) *$/ (\1)/' I'm piping titles through this from
This question is pretty much the same as this .Net question exept for java.
This is related to this question . I'm not an expert on Linux device

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.