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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:36:41+00:00 2026-05-11T07:36:41+00:00

I’m writing C cross-platform library but eventually I’ve got error in my unittests, but

  • 0

I’m writing C cross-platform library but eventually I’ve got error in my unittests, but only on Windows machines. I’ve tracked the problem and found it’s related to alignment of structures (I’m using arrays of structures to hold data for multiple similar objects). The problem is: memset(sizeof(struct)) and setting structures members one by one produce different byte-to-byte result and therefore memcmp() returns ‘not equal’ result.

Here the code for illustration:

#include <stdio.h> #include <string.h>  typedef struct {     long long      a;     int            b; } S1;  typedef struct {     long           a;     int            b; } S2;  S1 s1, s2;  int main() {     printf('%d %d\n', sizeof(S1), sizeof(S2));      memset(&s1, 0xFF, sizeof(S1));     memset(&s2, 0x00, sizeof(S1));      s1.a = 0LL; s1.b = 0;      if (0 == memcmp(&s1, &s2, sizeof(S1)))         printf('Equal\n');     else         printf('Not equal\n');      return 0; } 

This code with MSVC 2003 @ Windows produce following output:

16 8 Not equal 

But the same code with GCC 3.3.6 @ Linux works as expected:

12 8 Equal 

This makes my unit-testing very hard.

Am I understand correctly that MSVC uses size of biggest native type (long long) to determine alignment to structure?

Can somebody give me advice how can I change my code to make it more robust against this strange alignment problem? In my real code I’m working with arrays of structures via generic pointers to execute memset/memcmp and I’m usually don’t know exact type, I have only sizeof(struct) value.

  • 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. 2026-05-11T07:36:41+00:00Added an answer on May 11, 2026 at 7:36 am

    What we have done is used the #pragma pack to specify how big the objects should be:

    #pragma pack(push, 2)  typedef struct {     long long      a;     int            b; } S1;  typedef struct {     long           a;     int            b; } S2;  #pragma pack(pop) 

    If you do this, the structures will be the same size on both platforms.

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

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.