Member layout of structures and type convertions

L

lovecreatesbeauty

Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes from variable typed
datetime to variable typed dtime using memcpy(), can I get the value of
fields in struct datetime through dtime.dt? For example:

datetime *pdate = (datetime *)malloc(sizeof(*pdate));
dtime sdate;
/* initializaion omitted */
memcpy(&sdate, pdate, sizeof(*pdate));

then, are the following conditional expressions true?

sdate.dt[0] == pdate->dt_qual;
sdate.dt[1] == pdate->dec_exp;
sdate.dt[2] == pdate->dec_pos;
sdate.dt[3] == pdate->dec_ndgts;

2. Type convertions are valid for many data types, but why convertions
(even explicitly) between structs (non-scalar types?...) are forbiden?
What's the consideration under the limitation?
 
B

Barry Schwarz

Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes from variable typed

Yes, by copying from member to member.
datetime to variable typed dtime using memcpy(), can I get the value of
fields in struct datetime through dtime.dt? For example:


No, this is not portable. You are guaranteed that dt and dt_qual both
are mapped at the beginning of their respective structures. You are
also guaranteed that dt[1] follows dt[0] immediately. You are not
guaranteed that dec_exp follows dt_qual with no intervening padding.
datetime *pdate = (datetime *)malloc(sizeof(*pdate));

Don't cast the return from malloc. It seldom helps and can hide a
diagnostic you really want to see.
dtime sdate;
/* initializaion omitted */
memcpy(&sdate, pdate, sizeof(*pdate));

There is no guarantee that *pdate and sdate have the same size.
then, are the following conditional expressions true?

sdate.dt[0] == pdate->dt_qual;
sdate.dt[1] == pdate->dec_exp;
sdate.dt[2] == pdate->dec_pos;
sdate.dt[3] == pdate->dec_ndgts;
No.


2. Type convertions are valid for many data types, but why convertions
(even explicitly) between structs (non-scalar types?...) are forbiden?
What's the consideration under the limitation?

See above. Furthermore, each structure declaration creates a new
unique type.


<<Remove the del for email>>
 
E

EventHelix.com

Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes from variable typed
datetime to variable typed dtime using memcpy(), can I get the value of
fields in struct datetime through dtime.dt? For example:

2. Type convertions are valid for many data types, but why convertions
(even explicitly) between structs (non-scalar types?...) are forbiden?
What's the consideration under the limitation?

Converting between the two structures is not portable. The compiler
might have inserted extra pad bytes. In your specific example this is
unlikely but such conversions are not recommended.

The following article might help:

http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm
 
D

Dave Thompson

Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes <snip>

Elements of an array are required to be contiguous; as others have
noted, members of a struct are not, although there is no good reason
an implementation should add padding between members of the same type
as here, and I know of none that does.

I would add however that your commented-out part, assuming 2 ints
"cover" 8 characters, is also not guaranteed and will in fact fail on
quite a few real implementations.

- David.Thompson1 at worldnet.att.net
 

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

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top