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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:00:36+00:00 2026-05-20T19:00:36+00:00

The following is an excerpt from a code sample in K&R’s The C Programming

  • 0

The following is an excerpt from a code sample in K&R’s The C Programming Language (section 8.7):

typedef long Align;

union header {
    struct {
        union header *ptr;
        unsigned size;
    } s;
    Align x;
};

typedef union header Header;

And here is the explanatory excerpt:

To simplify alignment, all blocks are
multiples of the header size, and the
header is aligned properly. This is
achieved by a union that contains the
desired header structure and an
instance of the most restrictive type,
which we have arbitrarily made a long.

So, as I understand it, the idea is to ensure that a header instance takes up a number of bytes that is a multiple of sizeof(Align). The goal makes perfect sense to me.

But consider the case where long is 64 bits, int is 32 bits, and a pointer is 64 bits. In that case, header.s will be 64+32 = 96 bits. Thus, sizeof(header) will be 96 bits, which is not a multiple of 64 as intended.

In such a case, I suppose it would be necessary to define Align as something else (perhaps double). But I’m not quite sure whether I’m fully understanding what would dictate that choice for a particular architecture. Is it just the “biggest” type? And what if long is the biggest type – couldn’t it fail to work in the way I described above?

Thanks

  • 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-20T19:00:37+00:00Added an answer on May 20, 2026 at 7:00 pm

    The compiler will adjust the size of header so that ptr, size, and x can all be retrieved efficiently from elements in a header[] array. On most architectures, the presence of x means that sizeof(header) will be increased to a multiple of sizeof(long).

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

Sidebar

Related Questions

The following code sample is an implementation of the Strategy pattern copied from Wikipedia
I have the following HTML (excerpt from larger code-base) <div class=diary-event ui-corner-all title=[title]> <span
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin
In the following config file excerpt, the WCF service has two endpoints. <service behaviorConfiguration=AtomTcpHub.Behavior
Following on from my recent question on Large, Complex Objects as a Web Service
Following on from this question what would be the best way to write a
Following techniques from 'Modern C++ Design', I am implementing a persistence library with various
From what I've seen in the past, StackOverflow seems to like programming challenges, such

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.