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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:14:16+00:00 2026-06-13T19:14:16+00:00

I’m using x86 intel machine, and windows 7, plus Visual C++ (versions 2005/2012 express)

  • 0

I’m using x86 intel machine, and windows 7, plus Visual C++ (versions 2005/2012 express)

I have been playing with alignment (I have just been doing this as a learning exercise.) Certainly I understand the implications on the size of the class/struct in terms of padding. I believe that I understand that it is also better aligned because of the way that CPU instructions work and expect data.

I have been looking at many different resources generally, for example (interesting)
c++ data alignment /member order & inheritance
(and other links like wikipedia) http://en.wikipedia.org/wiki/Data_structure_alignment

One area that can be affected (I read) seems to be performance, due to the need for data to be specific sizes for registers, misaligned data can cause issues (see wikipedia).

I wrote some code in which I created 3 structs, all with the same members with packing set to 1 , normal alignment, and with the members rearranged. This gave me objects with sizeof 8, 10 and 12. I ran code similar to the following for each :

struct MixedData1
{
    char Data1;
    short Data2;
    int Data3;
    char Data4;

    void operator() (MixedData1& md)
    {
        md.Data1 = 'a';
        md.Data2 = 1024;
        md.Data3 = 1000000;
        md.Data4 = 'b';
    }
};

typedef std::vector<MixedData1> MDVector;


int main(int argc, char* argv[])
{
    MixedData1 md;
    for(int count = 0; count < 10 ; count++)
    {    
        {
        std::cout << sizeof(md) << std::endl;
        boost::timer::auto_cpu_timer t;
        MDVector mdv(10000000); 
        std::fill(mdv.begin(),mdv.end(),md );
        std::for_each(mdv.begin(),mdv.end(),md);
        }
    }
}

I’m not really interested in the values so each element in the vector is initialised the same. Anyway I got results that indicated that the running time increased with the size of the struct – I.E with pack(1) (8 bytes) I got the quickest 0.08s, and with normal alignment (12 bytes) I got the slowest 0.105 .

My questions are about the effects of being wrongly aligned. I don’t think I have ever had any issues with alignment throughout my X years as a C++ programmer, but of course it could have just passed me by.

(1) The alignment had an effect (I believe) in my test (edit) however as Neil posted it was only due to the difference in struct size. I tried accessing the member as per his reply but I saw no real effect there…. is there a clearer example? Is there a way I can see a dramatic effect of misalignment?
(2) Is there a way to induce a crash caused by misalignment if possible.

  • 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-13T19:14:17+00:00Added an answer on June 13, 2026 at 7:14 pm

    All your code does is to test to see how quickly the processor can copy memory. The more memory, the slower the copy. The alignment of the individual members within the structure is irrelevant to the speed of the copy, only the size of the structure matters.

    If you want to see the effect of the alignment, you need to write code that actually access individual unaligned structure members. For instance, you could write a loop to increment the data3 members of each structure. Depending on the architecture the compiler may realise that it has to use different instructions to perform the arithmetic; on x86 this is usually not the case and the compiler will emit natural looking code because the processor is capable with dealing with unaligned accesses. Some processors can actually read and write unaligned data at the same speed as aligned data. A trivial example of this is the 8088 as it only has an 8-bit data bus so all 16-bit instructions are emulated using two loads anyway, but the latest processors spend most of their time reading from cache lines and so the only time unaligned data might make a difference is when the data crosses a cache line.

    If you want to induce a crash by misalignment then normally you need to cast pointers between different types. The compiler then may not always realise that your pointer may be misaligned and will not generate the correct instructions for a misaligned access. For instance you could attempt to invoke an SSE instruction on a cast char* pointer.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I

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.