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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:28:55+00:00 2026-06-10T10:28:55+00:00

Consider the following program #include <cstdio> #include <stdint.h> struct Foo { int32_t a; int32_t

  • 0

Consider the following program

#include <cstdio>
#include <stdint.h>

struct Foo
    {
    int32_t a;
    int32_t b;
    int32_t c;
    };

struct Bar
    {
    int32_t a;
    int32_t b;
    int16_t c;
    };

int main()
    {
    printf("sizeof(Foo)=%u\nsizeof(Bar)=%u",sizeof(Foo),sizeof(Bar));
    return 0;
    }

When compiling using MinGW I get sizeof(Foo)=12 and sizeof(Bar)=12 on both 32-bit and 64-bit systems (I guess that in a 16-bit environment, sizeof(Bar)=10). Is it likely that in the future, the size of these structures will be 16 to make the fit better in the 64-bit environment?

I’m storing structs in files, and do not want to add work-arounds in the future. The Doom thing structure layout was probably a good choice 1993 but not today.

  • 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-10T10:28:56+00:00Added an answer on June 10, 2026 at 10:28 am

    The size of the Bar structure has little to do with whether you’re on a 16-bit environment, it has solely to do with the alignment requirements of the fields within your structure.

    The alignment of the structure is generally identical to the most restrictive alignment of all of the fields within it. In this case, it’s the 32-bit alignment requirement of an int32_t type that’s affecting it.

    Now you may think that shouldn’t be the case since the final field is a 16-bit value and there’s nothing after it requiring further alignment, but you’d be wrong. Think of what happens with an array of those things:

    0000  index 0  a      32 bits
    0004           b      32 bits
    0008           c      16 bits
    000a           filler 16 bits
    000c  index 1  a      32 bits
    0010           b      32 bits
    0014           c      16 bits
    0018           filler 16 bits
    

    You can see that filler is required after c due to the requirement for the following a to be correctly aligned.

    In terms of the future, it would be unlikely that any of the alignments of those fields would change, simply because they are exact bit-width types. They will always be 32-bit values and probably always require a 32-bit alignment.

    Having said that, there’s no guarantee that you won’t find an implementation that requires 32 bit values to be aligned on 128-bit boundaries at some point in the future. Section 6.2.8 Alignment of objects is the controlling section in C11, and it doesn’t seem to discount this possibility since it doesn’t state the the alignment is related to the object size:

    An object type imposes an alignment requirement on every object of that type: stricter alignment can be requested using the _Align keyword.

    If you want to catch the possibility that the structure size and/or alignment changes, you can use sizeof and alignof in your main function to output a message and exit if they’re not correct. This won’t affect code in the field but will help enormously if you move to a compiler with stricter alignments.

    And that’s the point where you change it (see YAGNI). The problem with planning for all possible futures is that you have no idea what will happen 🙂

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

Sidebar

Related Questions

Consider the following small program: #include <cstdio> int main() { printf(%d\n, 1); std::printf(%d\n, 2);
Consider the following program. #include <stdio.h> int main() { int a[10]={0}; printf(%p %p\n, a,
Consider the following program: #include <stdio.h> int main(void) { return 0; } When i
Lets us consider the following program : #include <stdlib.h> int main(int argc, char **argv){
Consider the following program, which is obviously buggy: #include <cstdio> double test(int n) {
Consider the following program: #include <iostream> #include boost/filesystem.hpp int main() { boost::filesystem::directory_entry d(test.txt); boost::filesystem::directory_entry
Consider the following C program: #include <stdio.h> int main(){ int a =-1; unsigned b=-1;
Consider the following C++ program: #include <memory> struct A {}; struct B : A
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following program: $x=12345678901.234567000; $y=($x-int($x))*1000000000; printf(%f:%f\n,$x,$y); Here's what is prints: 12345678901.234568:234567642.211914 I was

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.