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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:23:49+00:00 2026-05-20T18:23:49+00:00

I am using a library that has a function that takes an array of

  • 0

I am using a library that has a function that takes an array of structs. That struct and function has the following layout:

struct TwoInt32s
{
  int32_t a;
  int32_t b;
};

void write(struct TwoInt32s *buffer, int len);

My initial tests suggest that an array of such structs has the same memory layout as an array of int32_t so I can do something like this:

int32_t *buffer = malloc(2 * len * sizeof(int32_t));
/* fill in the buffer */
write((struct TwoInt32s*)buffer, len);

However I’m wondering if this is universally true or not. Using an array of int32_t greatly simplifies my code.

EDIT: I forgot the sizeof

From what I read, C guarantees a few things about struct padding:

  1. members will NOT be reordered
  2. padding will only be added between members with different alignments or at the end of the struct
  3. a pointer to a struct points to the same memory location as a pointer to its first member
  4. each member is aligned in a manner appropriate for its type
  5. there may be unnamed holes in the struct as necessary to achieve alignment

From this I can extrapolate that a and b have no padding between them. However it’s possible that the struct will have padding at the end. I doubt this since it’s word-aligned on both 32 and 64 bit systems. Does anyone have additional information on this?

  • 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-05-20T18:23:50+00:00Added an answer on May 20, 2026 at 6:23 pm

    The implementation is free to pad structs – there may be unused bytes in between a and b. It is guaranteed that the first member isn’t offset from the beginning of the struct though.

    Typically you manage such layout with a compiler-specific pragma, e.g:

    #pragma pack(push)
    #pragma pack(1)
    struct TwoInt32s
    {
      int32_t a;
      int32_t b;
    };
    #pragma pack(pop)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a library that has headers without the .h This defeats visual
Using ICU 4.0 regex library, I find that the following regex is exhibiting exponential
In C#, I am using a library that defines an enum. I would like
I'm using a third-party library that returns XML that is not valid, because it
I have a shared library that I wish to link an executable against using
Is there any feasible way of using generics to create a Math library that
I'm implementing a cache in a class library that i'm using in an asp.net
I am working on a very large scale computing library that is using STL
I was using mysql++ library and compiling with GCC 3.3.4. That GCC version had
I have a legacy C++ module that offers encryption/decryption using the openssl library (DES

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.