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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:38:14+00:00 2026-06-08T04:38:14+00:00

Consider the following C++ code that prints the alignment requirement for a double and

  • 0

Consider the following C++ code that prints the alignment requirement for a double and the effective alignment of a structure member of type double:

#include <iostream>

struct S { short x; double y; };

int main() {
    S s;
    std::cout << __alignof(double) << ' ';
    std::cout << (char*)&s.y - (char*)&s << std::endl;
}

(Run on codepag.org)

I’m compiling this code with gcc on x86.

I was expecting this program to output 8 8, however it prints 8 4 instead. Why is that?

  • 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-08T04:38:15+00:00Added an answer on June 8, 2026 at 4:38 am

    The alignment of an object can be different outside and inside a structure. Outside the structure it might be the smallest alignment that allows the fastest access (for example 8 byte). But inside the structure it could be the some value which needs the least padding (4 bytes on x86). Some compilers allow you to control the structure packing (gcc:__attribute__((pack)), microsoft: #pragma pack())

    What exactly the compiler does, it determined by the ABI of your system. If the ABI says structure values have no larger padding than four bytes, then the compiler will conform to this.

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

Sidebar

Related Questions

Consider the following code snippet: #include <iostream> using namespace std; struct Element { void
Consider the following code sample that uses MEF to create an object of type
Consider the following 2 code snippets: Case 1: #include <iostream> int main() { int
Consider the following code that automates generation of Boost.MPL type sequences (list or vector).
Consider this following code (that retrieves the response from a HTTP request and prints
Consider the following code: #include <stdio.h> int main(void) { int a[10]; printf(%d,(int)sizeof(a)); //prints 10*sizeof(int)
Why isn't setjmp saving the stack? Consider the following code: #include <iostream> jmp_buf Buf;
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Consider the following code and assume that list is an synchronized List. List list
Consider the following code. It's an anchor tag with an id called leader-module-total that

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.