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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:43:06+00:00 2026-06-10T13:43:06+00:00

What is the best way to make a non-packed non-padded struct in C or

  • 0

What is the best way to make a non-packed non-padded struct in C or C++? The goal is:

typedef struct {
    char first[3]; //padded to 8 because next type has size 8
    double second;
    char third;
} SomeStruct;

but do something to make sizeof(SomeStruct) be equal 17;

The structure is defined by external library specification. I have a 2 decisions:

1)

#pragma pack (push, 1)

typedef struct {
    char first[3];
        char unused[5]; // Manual padding of second field.
    double second;
    char third;
} SomeStruct;

#pragma pack (pop)

2) Using the first variant of declaration, but write (int)(&((SomeStruct*)0)->third) + sizeof(((SomeStruct*)0)->third) instead of sizeof(SomeStruct) when serializing data. I don’t need to serialize arrays of such structs, so can do that.

But both of them are dirty hacks. Is there a standard approach of removing the structs padding in C?

  • 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-10T13:43:07+00:00Added an answer on June 10, 2026 at 1:43 pm

    C11 now has the _Alignas keyword that could suggest a more narrow alignment of your double

    typedef struct {
        char first[3]; //padded to 8 because next type has size 8
        _Alignas(uint16_t) double second;
        char third;
    } SomeStruct;
    

    This would do narrower aligment whenever that is possible, that is if the standard operators for double on your platform are able to cope with such an alignment. (If not you are screwed anyhow.)

    Clang already implements that feature, and other compilers such as gcc already have extensions that come close to it, so you can easily write yourself a wrapper that uses this syntax and is “future proof”.

    Edit: I only saw after posting that your question is also about C++. I think C++1 has the same feature as alignas. (C11 has alignas -> _Alignas via a macro from a standard header file.)

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

Sidebar

Related Questions

What's the best way to make a function that has pointer as argument work
How I can make a Makefile, because it's the best way when you distribute
What is the best way to make the size of box 2 equal to
what's the best way to make communications between page widgets residing on differents ViewPart?
What is the best way to make an HTTP GET request in Ruby with
What is the best way to make sure that all the environment variables I
What is the best way to make my Qt Ruby application into an executable
I'm wondering what is the best way to make data thread-safe. Specifically, I need
I am wondering whats the best way to programmatically make rounded corners for images.
Possible Duplicate: ASP.Net:Best way to run scheduled tasks I have to make a scheduled

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.