Microsoft KB Archive/40027

From BetaArchive Wiki
Knowledge Base


INFO: #pragma pack() Affects Declarations, Not Definitions

Article ID: 40027

Article Last Modified on 7/5/2005



APPLIES TO

  • Microsoft C Professional Development System 6.0a
  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 1.5 Professional Edition
  • Microsoft Visual C++ 1.51
  • Microsoft Visual C++ 1.52 Professional Edition
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q40027

SUMMARY

The pack pragma affects declarations, not definitions. Place #pragma pack() prior to data declarations.

MORE INFORMATION

Sample Code

The following program demonstrates usage of #pragma pack():

/* compile options needed */ 
/* Elements of variables of type struct x will be byte-aligned. */ 
#pragma pack(1)
struct x { int a; char b; int c; };

/* Elements of variables of type struct y will be word-aligned. */ 
#pragma pack(2)
struct y { int a; char b; int d; };

/* The pragma below does NOT affect the definitions that follow. */ 
#pragma pack(4)

struct x X;
struct y Y;

void main (void)
{ /* dummy main */ }
                

NOTE: The default packing value for the 16-bit products mentioned above is 2 bytes. The default packing value for Visual C++ 32-bit, versions 1.0 and 2.xx, is 4 bytes. The default packing value for Visual C++ 32-bit, version 4.0, is 4 bytes for bit fields and 8 bytes otherwise.

Keywords: kbinfo kblangc KB40027