char c; int a;

};

#pragma pack

PS1::PS1(): a(1) { // There appears to be no pointer, but there

//is an unaligned access, possibly through “this.” printf(“In constructor.\n”);

}

PS1::~PS1() {

a = 0;// Misaligned access, possibly though “this” printf(“In destructor.\n”);

}

int main() {

#if defined(RECOVER) allow_unaligned_data_access();

#endif PS1 s;

}

UNALIGN

#pragma unalign [124816]

typedef T1 T2;

T1 and T2 have the same size and layout, but with specified alignment requirements.

HP aCC supports misaligned data access using the unalign pragma. The unalign pragma can be applied on typedef to define a type with special alignment. The unalign pragma takes effect only on next declaration.

If the unalign pragma declaration is not in the global scope or if it is not a typedef, compiler displays a warning message. If the specified alignment is greater than the original alignment of the declaration, then an error message is displayed, and the pragma is ignored.

Example:

#pragma unalign 1 typedef int ua_int; typedef ua_int *ua_intPtr;

//ua_int is of int type with 1 byte alignment

//this typedef is not affected by the above

//unalign pragma. It defines a pointer type

//which points to 1 byte aligned data

The interaction between pack and unalign pragmas is as follows:

#pragma pack 1

 

struct

S {

 

char c;

 

int

i;

 

};

 

 

#pragma pack 0

 

S s;

 

 

ua_int

*ua_ip = &s.i;

// ua_ip points to 1 byte

 

 

// aligned int

*ua_ip

= 2;

// mis-aligned access to

 

 

// 1 byte aligned int

NOTE: The HP_ALIGN pragma, which is supported by the HP ANSI C compiler, is not supported by aCC. The pack and unalign pragmas can replace most of the HP_ALIGN functionality.

102 Pragma Directives and Attributes

Page 102
Image 102
HP C/aC++ for PA-RISC Software #pragma unalign Typedef T1 T2, Interaction between pack and unalign pragmas is as follows