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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:59:58+00:00 2026-06-17T16:59:58+00:00

I have inherited some third-party code in a project. It’s comprised of a header

  • 0

I have inherited some third-party code in a project. It’s comprised of a header file and a binary library without sources, so I cannot just recompile it with different settings.

The header uses a compile-time assert-like trick (typedefing a negative-size array if expression fails) to ensure proper struct alignment, but it doesn’t seem to work in 32-bit mode.

Here’s a stand-alone little program that captures the entire problem:

#include <stdio.h>
#include <stddef.h>

#pragma pack(push, 8)
struct test {char _; long long a;};
typedef char pack_test[(offsetof(test, a) == 8) ? 1 : -1];
#pragma pack(pop)

int main(int argc, char *argv[])
{
        printf("%d\n", offsetof(test, a));
        return 0;
}

The code above fails to compile both on clang 3.0 and on any recent gcc version I could quickly lay my hands on (4.5 to 4.7): the packing pragmas simply have no effect whatsoever. The compiler keeps aligning the member a to 4 bytes (you can check that by commenting the typedef out).

Why is that? How can I fix the code so that this assertion doesn’t fail and keep it compatible with the ABI while not having to go over all the structs in the header and tacking attributes onto their tails?

  • 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-17T16:59:59+00:00Added an answer on June 17, 2026 at 4:59 pm

    On a 32-bit system, a long long may not need an alignment of eight bytes, four should be enough. After all, you still fetch two entire words from memory, whether the first of them is eight-byte aligned or four.

    You can try to force eight byte alignment with an attribute

    struct test { char _; long long a __attribute__ ((aligned(8))); };
    

    (probably wrong, I’m not familiar with gcc’s attributes)

    Of course, you can also add a dummy member to the struct to ensure the desired alignment

    struct test { char _; char dummy[7]; long long a; }
    

    That should work in practice.

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

Sidebar

Related Questions

I have inherited some code (from zip file) from a developer and git initialzed,
I have inherited some Classic ASP code and I need a JSON library that
I have inherited some HTML code and have been asked to align the two
I have inherited some code: Process p = new ProcessBuilder(/bin/chmod, 777, path).start(); p.waitFor(); Basically,
I have a third party library that internally constructs and uses the SqlConnection class.
I have inherited some legacy PHP code what was written back when it was
I have inherited some old code and need to convert to the latest schema
I have inherited some legacy vb6 code. It's a tool which generates a local
I have inherited some bloody awful code that I am trying to debug. I'm
I have inherited some code below that works fine, the only problem is, 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.