HighTechTalks DotNet Forums  

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

VC++.net microsoft.public.dotnet.languages.vc


Discuss How can I used packed typdefs in Visual C/C++ in the VC++.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
das
 
Posts: n/a

Default How can I used packed typdefs in Visual C/C++ - 12-22-2008 , 01:53 PM






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

Reply With Quote
  #2  
Old   
Cholo Lennon
 
Posts: n/a

Default Re: How can I used packed typdefs in Visual C/C++ - 12-22-2008 , 02:10 PM






das wrote:
Quote:
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

--
Cholo Lennon
Bs.As.
ARG





Reply With Quote
  #3  
Old   
Pavel A.
 
Posts: n/a

Default Re: How can I used packed typdefs in Visual C/C++ - 12-22-2008 , 02:57 PM



das wrote:
Quote:
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


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.