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

The Archive Base Latest Questions

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

Our headers use #pragma pack(1) around most of our structs (used for net and

  • 0

Our headers use #pragma pack(1) around most of our structs (used for net and file I/O). I understand that it changes the alignment of structs from the default of 8 bytes, to an alignment of 1 byte.

Assuming that everything is run in 32-bit Linux (perhaps Windows too), is there any performance hit that comes from this packing alignment?

I’m not concerned about portability for libraries, but more with compatibility of file and network I/O with different #pragma packs, and performance issues.

  • 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-26T05:12:56+00:00Added an answer on May 26, 2026 at 5:12 am

    Memory access is fastest when it can take place at word-aligned memory addresses. The simplest example is the following struct (which @Didier also used):

    struct sample {
       char a;
       int b;
    };
    

    By default, GCC inserts padding, so a is at offset 0, and b is at offset 4 (word-aligned). Without padding, b isn’t word-aligned, and access is slower.

    How much slower?

    • For 32-bit x86, according to the Intel 64 and IA32 Architectures Software Developer’s Manual:

      The processor requires two memory
      accesses to make an unaligned memory access; aligned accesses require only one
      memory access. A word or doubleword operand that crosses a 4-byte boundary or a
      quadword operand that crosses an 8-byte boundary is considered unaligned and
      requires two separate memory bus cycles for access.

      As with most performance questions, you’d have to benchmark your application to see how much of an issue this is in practice.

    • According to Wikipedia, x86 extensions like SSE2 require word alignment.
    • Many other architectures require word alignment (and will generate SIGBUS errors if data structures aren’t word-aligned).

    Regarding portability: I assume that you’re using #pragma pack(1) so that you can send structs across the wire and to and from disk without worrying about different compilers or platforms packing structs differently. This is valid, however, there are a couple of issues to keep in mind:

    • This does nothing to handle big endian versus little endian issues. You can handle these by calling the htons family of functions on any ints, unsigned, etc. in your structs.
    • In my experience, working with packed, serializable structs in application code isn’t a lot of fun. They’re very difficult to modify and extend without breaking backwards compatibility, and as already noted, there are performance penalties. Consider transferring your packed, serializable structs’ contents into equivalent non-packed, extensible structs for processing, or consider using a full-fledged serialization library like Protocol Buffers (which has C bindings).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the bigpipe concept on our website. That means trying
All of our C++ headers use a .h extension. Eclipse thinks these are C
At the moment we set content-type in our responce headers, a bit like this...
OK, I'm having aplay with expires headers in IIS6 on our development server and
Our PHP application makes use of json_encode($myObject) a lot, in conjunction with the mustache
Our application is a Java-GWT application that uses Guice-Persist and Guice-Servlet extensively. We have
Our asp.net web site is currently deployed to an IIS7 server. We are setting
We've identified that full browser caches are the cause of a problem on our
I've been asked to prototype a replacement file transformation process (that currently is a
I have written a C++ app that sync's Outlook emails with our central server

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.