How can I used packed typdefs in Visual C/C++

D

das

I wanto use a packed structure with a malloc. But the pack pragma only
applies to data declarations not typdef. is this possible in C/C++?

eg I want do something like;

#define PACK_UNIX __attribute__((aligned))
#define Iu32 to be a 4 byte variable type.

typedef struct thread_trace_element1 {
Iu32 timestamp PACK_UNIX ;
Iu32 tracepoint PACK_UNIX ;
Iu32 type PACK_UNIX ;
Iu32 p1 PACK_UNIX ;
Iu32 p2 PACK_UNIX ;
Iu32 p3 PACK_UNIX ;
Iu32 p4 PACK_UNIX ;
} thread_trace_element1_t;

typedef struct thread_trace_element2 {
Iu32 timestamp PACK_UNIX ;
Iu32 tracepoint PACK_UNIX ;
Iu32 type PACK_UNIX ;
Iu32 mode PACK_UNIX ;
Iu32 handle PACK_UNIX ;
Iu32 rc PACK_UNIX ;
Iu32 p4 PACK_UNIX ;
} thread_trace_element2_t;


typedef union u_thread_trace_element {
thread_trace_element1_t;
thread_trace_element2_t
} u_thread_trace_element_t;

and then

void *p;

p = malloc( sizeof(u_thread_trace_element_t ))

so that I can then use

thread_trace_element1_t *q;
thread_trace_element2_t; *r;

q = p;
r = p;

and then use

q->p1 and r->mode to refer to the same 4 byte value
 
C

Cholo Lennon

das said:
I wanto use a packed structure with a malloc. But the pack pragma
only applies to data declarations not typdef. is this possible in
C/C++?

eg I want do something like;

#define PACK_UNIX __attribute__((aligned))
#define Iu32 to be a 4 byte variable type.

typedef struct thread_trace_element1 {
Iu32 timestamp PACK_UNIX ;
Iu32 tracepoint PACK_UNIX ;
Iu32 type PACK_UNIX ;
Iu32 p1 PACK_UNIX ;
Iu32 p2 PACK_UNIX ;
Iu32 p3 PACK_UNIX ;
Iu32 p4 PACK_UNIX ;
} thread_trace_element1_t;

typedef struct thread_trace_element2 {
Iu32 timestamp PACK_UNIX ;
Iu32 tracepoint PACK_UNIX ;
Iu32 type PACK_UNIX ;
Iu32 mode PACK_UNIX ;
Iu32 handle PACK_UNIX ;
Iu32 rc PACK_UNIX ;
Iu32 p4 PACK_UNIX ;
} thread_trace_element2_t;


typedef union u_thread_trace_element {
thread_trace_element1_t;
thread_trace_element2_t
} u_thread_trace_element_t;

and then

void *p;

p = malloc( sizeof(u_thread_trace_element_t ))

so that I can then use

thread_trace_element1_t *q;
thread_trace_element2_t; *r;

q = p;
r = p;

and then use

q->p1 and r->mode to refer to the same 4 byte value

Just use __declspec(align(X)) with typedefs

http://msdn.microsoft.com/en-us/library/83ythb65.aspx

Regards
 
P

Pavel A.

das said:
I wanto use a packed structure with a malloc. But the pack pragma only
applies to data declarations not typdef. is this possible in C/C++?

eg I want do something like;

#define PACK_UNIX __attribute__((aligned))
#define Iu32 to be a 4 byte variable type.

typedef struct thread_trace_element1 {
Iu32 timestamp PACK_UNIX ;
Iu32 tracepoint PACK_UNIX ;
Iu32 type PACK_UNIX ;
Iu32 p1 PACK_UNIX ;
Iu32 p2 PACK_UNIX ;
Iu32 p3 PACK_UNIX ;
Iu32 p4 PACK_UNIX ;
} thread_trace_element1_t;

typedef struct thread_trace_element2 {
Iu32 timestamp PACK_UNIX ;
Iu32 tracepoint PACK_UNIX ;
Iu32 type PACK_UNIX ;
Iu32 mode PACK_UNIX ;
Iu32 handle PACK_UNIX ;
Iu32 rc PACK_UNIX ;
Iu32 p4 PACK_UNIX ;
} thread_trace_element2_t;


typedef union u_thread_trace_element {
thread_trace_element1_t;
thread_trace_element2_t
} u_thread_trace_element_t;

and then

void *p;

p = malloc( sizeof(u_thread_trace_element_t ))

so that I can then use

thread_trace_element1_t *q;
thread_trace_element2_t; *r;

q = p;
r = p;

and then use

q->p1 and r->mode to refer to the same 4 byte value

These "PACK_UNIX" macro look very confusing and suspicious to me.
Your structs consist of all 4-byte integers (u32 or __u32 or UINT32),
They will be automatically packed and aligned.
Address returned by malloc is aligned enough for all types.
So it will just work.

Packing is needed only if you want to avoid automatic alignment
on the variable size and instead use lesser alignment.
For MS compiler, #pragma pack does this,
for GNU (off my head) __attribute__((pack))

Regards,
--PA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top