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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:44:17+00:00 2026-06-02T09:44:17+00:00

Visual C++ offers both a compiler switch ( /Zp ) and the pack pragma

  • 0

Visual C++ offers both a compiler switch (/Zp) and the pack pragma to affect the aligment of struct members. However, I seem to have some misconception as to how they work.

According to MSDN, for a given alignment value n,

The alignment of a member will be on a boundary that is either a
multiple of n or a multiple of the size of the member, whichever is
smaller.

Let’s assume a pack value of 8 bytes (which is the default). Within a struct, I’d think that any member whose size is less than 8 bytes will be at an offset that is a multiple of its own size. Any member whose size is 8 bytes or more will be at an offset that is a multiple of 8 bytes.

Now take the following program:

#include <tchar.h>

#pragma pack(8)

struct Foo {
    int i1;
    int i2;
    char c;
};

struct Bar {
    char c;
    Foo foo;
};

int _tmain(int argc, _TCHAR* argv[]) {
    int fooSize = sizeof(Foo); // yields 12
    Bar bar;
    int fooOffset = ((int) &bar.foo) - ((int) &bar); // yields 4

    return 0;
}

The Foo structure is 12 bytes in size. So within Bar, I’d expect the Foo member to be at offset 8 (a multiple of 8) while actually it’s at offset 4. Why is that?

Also, Foo really only has 4+4+1 = 9 bytes of data. The compiler automatically adds padding bytes at the end. But again, given an alignment value of 8 bytes, shouldn’t it pad to a multiple of 8 rather than 4?

Any clarification appreciated!

  • 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-02T09:44:18+00:00Added an answer on June 2, 2026 at 9:44 am

    Your excerpt explains this, “whichever is smaller”. On a 32-bit platform, an int is 4 bytes. 4 is smaller than 8. So it has a 4-byte alignment.

    The pack pragma causes things to be packed, not unpacked. It won’t pad unless it has a reason to.

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

Sidebar

Related Questions

Visual Studio 2010 offers a lot of comfortable tools for unit testing via its
When I create a ASP.NET MVC web application in Visual Studio 2010 it offers
I have an application that I'm developing in Visual Studio 2010 that utilizes a
I have a couple of question's regarding the following project. *Scott, Recently some developers
As some of you know the RockScroll Visual Studio plugin is pretty darn awesome.
So Visual Studio offers MVC content in context menus for Add View, etc... Sure,
I have installed the new Visual Studio 2012 Ultimate. I have created a Test
Visual Studio Team System and Team Foundation Server offers a lot of goodies like
I'm a developer who works on both individual and group projects using Microsoft Visual
On a recent project I have been working on in C#/ASP.NET I have some

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.