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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:06:09+00:00 2026-06-05T01:06:09+00:00

I’m trying to pack data in a c++ struct. My struct has this layout:

  • 0

I’m trying to pack data in a c++ struct.

My struct has this layout:

struct structName
{
  int16_t member1;
  int32_t member2;
  uint32_t member3;
  uint32_t member4;
  uint32_t member5;
  etc
}__attribute__((packed));

Using offsetof($structname, $membername) I get back the correct offsets of the data (0,2,6,10,14 . . .), but when I access the data by member-name I get the data at 4 byte offsets (0,4,8,12,16 . . .) as if the struct wasn’t packed.

Is

} __attribute__((packed));

the correct way to make a struct packed?
.
.

  • 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-05T01:06:12+00:00Added an answer on June 5, 2026 at 1:06 am

    Update: mydogisbox wrote:

    For the record, __attribute__((packed)), #pramga pack(1) and #pragma pack(push, 1) all worked.

    __attribute__((packed)) is a gcc extension, which is supported.

    The clang documentation says it also supports #pragma pack(...) directive:

    clang has some experimental support for extensions from Microsoft
    Visual C++; to enable it, use the -fms-extensions command-line option.
    This is the default for Windows targets. Note that the support is
    incomplete; enabling Microsoft extensions will silently drop certain
    constructs (including __declspec and Microsoft-style asm statements).

    clang supports the Microsoft #pragma pack feature for controlling record layout.

    source: http://clang.llvm.org/docs/UsersManual.html

    Just say:

    #pragma pack(1)
    struct my_struct {
        int16_t x;
        // etc.
    };
    

    to see if it works (compile with -fms-extensions if not using Windows).

    Note the above are all non-standard extensions, and the new C++11 standard has a new alignas keyword: http://en.cppreference.com/w/cpp/language/alignas

    struct alignas(1) my_struct {
        int16_t x;
        // etc.
    };
    

    but its support is still a bit sketchy.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.