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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:12:23+00:00 2026-06-15T23:12:23+00:00

I previously posted a question here about aligned access during pointer casting. As a

  • 0

I previously posted a question here about aligned access during pointer casting. As a summary, it’s better not to use unaligned access to be fully portable because some architecture may throw an exception, or the performance may get quite slower compared to aligned access.

However, there are cases that I want to use one-byte alignment, e.g., during transferring network data, I don’t want adding extra padding inside structure. So usually what’s done here is:

#pragma pack (push, 1)
struct tTelegram
{
   u8 cmd;
   u8 index;
   u16 addr1_16;
   u16 addr2_16;
   u8  length_low;
   u8 data[1];
};
#pragma pack (pop)

Then you might already know my question: If I enforce one-byte alignment on my struct, does that mean it cannot be fully portable, because struct members are not aligned? What if I want both no padding and portability?

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

    Firstly, misaligned memory accesses refers to single pieces of data that span multiple words in memory. For example: on a 32-bit system a 32-bit int at address 0, 4, 8 etc is aligned, but at 1, 2, 3, 5, 6, 7, 9 etc. would be misaligned.

    Secondly, misaligned data doesn’t “throw an exception” in the C++ sense, but may raise an interrupt/trap/exception at the CPU level – e.g. SIGBUS on UNIX, where you’d generally set a signal handler to react to this, but if you need to parse misaligned data in a portable way you wouldn’t do so by catching signals – you’d manually code the steps to pack and unpack data spanning word boundaries.

    In your tTelegram struct, the data is not “misaligned”, but the process of bit shifting and masking the data as it’s packed/unpacked from a register is still likely slower – requiring more machine code instructions – than using data that occupies an independent word.

    Regarding portability – all non-toy compilers will have an option to pack in the way you’ve described, but the exact pragma will vary, the layout of bytes in multi-byte values may still be big-endian or little-endian (or something plain weird), and while some CPUs allow some misaligned data access (e.g. x86) others don’t (e.g. Ultrasparc).

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

Sidebar

Related Questions

I posted a previous question here asking about what was better, JOIN queries or
I've seen this question posted here previously but I'm not satisfied that I understand
I have posted a question on here previously asking similar advise, but this project
I followed the question previously posted here I need to do pretty much same,
My question is similar to a question previously posted but never really answered here:
I'm rewording a question that I previously posted here . The default functionality for
I previously posted here: Controlling Access for Trial Subscription Since this is a new
Previously I posted a question regarding multithreading. Actually my intension is to send SMS
I'm writing code to use UIImagePickerController. Corey previously posted some nice sample code on
I posted one question previously and all of them answered that there is some

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.