HP C Programmer's Guide (92434-90009)

Chapter 2 29
Storage and Alignment Comparisons
The HP_ALIGN Pragma
c auto char SP-48
foo auto struct st SP-44
c member char 0x0 @ 0x0
i member int 0x4 @ 0x0
The resulting size of foo is 8 bytes, with 4-byte alignment.
If you change the type of member i in struct st to be a simple int type, then you will get
the following result:
main
Identifier Class Type Address
--
c auto char SP-40
foo auto struct st SP-38
c member char 0x0 @ 0x0
i member int 0x2 @ 0x0
This time, the resulting size of foo is 6 bytes, with 2-byte alignment.
Example 3: Incorrect Use of Typedefs and Alignments Often, you might mix
typedefs and alignments without being aware of the actual alignment of the data types.
What may appear to be correct usages of these data types may turn out to be causes for
misaligned pointers and run-time bus errors, among other things. For example, consider
the following program.
<my_include.h>
typedef unsigned short ushort;
extern int get_index(void);
extern ushort get_value(void);
<my_prog.c>
#include "my_include.h"
#pragma HP_ALIGN NOPADDING PUSH
struct s {
ushort member1;
ushort member2;
};
#pragma HP_ALIGN POP
char myBuffer[100];
int main()
{
struct s *my_struct;
int index = get_index();
int value = get_value();
int not_done = 1;
while (not_done) {
my_struct = (struct s*)&myBuffer[index];
my_struct->member1 = value;