I have the following structs defined. I ultimately want to copy UserInfo into a BYTE buffer to send to an RS232 serial port.
typedef struct
u_32 prot_ver;
u_32 nomem_ver
u_32 func_units;
u_32 sysType;
u_32 startupMode
AttributeList option_list;
AttributeList supported_aprofiles;
} UserInfo

typedef struct
u_16 attribute_id;
u_16 length;
u_16 attribute_val;
} AVAType

typedef struct
u_16 count;
u_16 length;
AVAType value[1]
} AttributeList

I have a buffe
msgBuffer = new BYTE [1000]

and i'm tryin to do
memcpy (msgBuffer, (BYTE *) &UserInfo, sizeof (UserInfo)

The first 5 fields copy, but the AttributeList struct field dont copy immediately behind the u_32 in my msgBuffer. :

I basically need the 'count' field of the AttributeList immediately behind the 'StartupMode' field

Some its not copying right. :( HELP?!?!?!?!??
-C

Re: Copying structs in BYTE buffers by Scott

Scott
Fri Apr 23 18:54:50 CDT 2004

Charles R wrote:

> I have the following structs defined. I ultimately want to copy UserInfo into a BYTE buffer to send to an RS232 serial port..
> typedef struct {
> u_32 prot_ver;
> u_32 nomem_ver;
> u_32 func_units;
> u_32 sysType;
> u_32 startupMode;
> AttributeList option_list;
> AttributeList supported_aprofiles;
> } UserInfo;
>
> typedef struct {
> u_16 attribute_id;
> u_16 length;
> u_16 attribute_val;
> } AVAType;
>
> typedef struct {
> u_16 count;
> u_16 length;
> AVAType value[1];
> } AttributeList;
>
> I have a buffer
> msgBuffer = new BYTE [1000];
>
> and i'm tryin to do
> memcpy (msgBuffer, (BYTE *) &UserInfo, sizeof (UserInfo);
>
> The first 5 fields copy, but the AttributeList struct field dont copy immediately behind the u_32 in my msgBuffer. :(
>
> I basically need the 'count' field of the AttributeList immediately behind the 'StartupMode' field.
>
> Some its not copying right. :( HELP?!?!?!?!??!
> -C

Your problem is structure packing. The compiler is permitted to insert
extra (unused) bytes into structs, which is usually a good thing (for
speed). You can turn it off for your struct declarations with:

#pragma pack(push, 1)
struct ... // your declarations here
#pragma pack(pop)

The '1' here forces the structs to be packed with 1-byte spacing, i.e.
no padding. The 'pop' is necessary to restore normal compiler behavior,
so your program will remain compatible with structs declared elsewhere,
such as in the windows header files.

--
Scott McPhillips [VC++ MVP]


Re: Copying structs in BYTE buffers by anonymous

anonymous
Sun Apr 25 10:01:05 CDT 2004

I already had the #pragma statements in there. I also have the compile switch /Zp1

Am I copying the struct inthe BYTE buffer wrong??? I'm totally thrown here

Do I need to turn something on?
-