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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:26:16+00:00 2026-05-24T08:26:16+00:00

struct { integer a; struct c b; … } In general how does gcc

  • 0
struct {
   integer a;
   struct c b;
   ...
}

In general how does gcc calculate the required space? Is there anyone here who has ever peeked into the internals?

  • 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-24T08:26:16+00:00Added an answer on May 24, 2026 at 8:26 am

    I have not “peeked at the internals”, but it’s pretty clear, and any sane compiler will do it exactly the same way. The process goes like:

    1. Begin with size 0.
    2. For each element, round size up to the next multiple of the alignment for that element, then add the size of that element.
    3. Finally, round size up to the least common multiple of the alignments of all members.

    Here’s an example (assume int is 4 bytes and has 4 byte alignment):

    struct foo {
        char a;
        int b;
        char c;
    };
    
    1. Size is initially 0.
    2. Round to alignment of char (1); size is still 0.
    3. Add size of char (1); size is now 1.
    4. Round to alignment of int (4); size is now 4.
    5. Add size of int (4); size is now 8.
    6. Round to alignment of char (1); size is still 8.
    7. Add size of char (1); size is now 9.
    8. Round to lcm(1,4) (4); size is now 12.

    Edit: To address why the last step is necessary, suppose instead the size were just 9, not 12. Now declare struct foo myfoo[2]; and consider &myfoo[1].b, which is 13 bytes past the beginning of myfoo and 9 bytes past &myfoo[0].b. This means it’s impossible for both myfoo[0].b and myfoo[1].b to be aligned to their required alignment (4).

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

Sidebar

Related Questions

Here TEST is a struct pointer. Structure contains integer. What can be the right
I need to do some large integer math. Are there any classes or structs
here is code which returns size of struct without using sizeof keyword #include <iostream>
I want to Create a NSMutableDictionary with an Integer Mapping to an strucuter(struct). Example:
First I write the integer using python: out.write( struct.pack(>i, int(i)) ); I then read
I'v created this class here: //Integer rectangle class class AguiRectangle { int x; int
Here's my code: struct RS_Token { char id; char cleanup; unsigned char array[sizeof (std::string)
Is there a way to strongly type integer ID values in C#? I've recently
I've got a struct with 2 integers, and I want to store them in
From a method, I can pass a struct which contains an array of integers,

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.