N
Ninan Thomas
Hi all
Reading various posts on the newsgroups i was under the impression
that memcpy copy the padding bytes for struct. Apparently it does not
do for me. Here is what i am trying to do
typedef struct DCIClassId {
int size;
unsigned char data; /* place holder for variable length data to follow
the struct*/
}DCIClassId;
this structure is followed by a variable no of integers, which is
stored immediatly after the structure. the size field determines the
number of integers
for example no of integers = size - sizeof(DCIClassId). The memory for
the integers to follow is allocated when the memory is allocated for
the structures
DCIClassId *classId;
cidp = (DCIClassId *)malloc (sizeof(DCIClassId) +
no_of_integers*sizeof(int));
I cannot change the struct definition as it is fixed by the standard
for the product i am working on.
My problem is I want to copy this structure into a character array to
be send over socket and retrieve it. I am using memcpy for it.
char sendmesg[4096]
memcpy(sendmesg + MSGID_SIZE , cidp, classId->size);
/* This is code following theoretically should on another process. But
i tested it also on the same process, to make sure it is not a network
issue.
*/
cidp = (DCIClassId *)malloc(classId->size);
memcpy(cidp, sendmesg + MSGID_SIZE, classId->size);
The integers following the struct are left shifted. and I am not able
to retrieve it. I store three integers following the struct. The
second integer is in the place of the first. The third integer is at
the location of second
I am using gcc 3.2
sizeof(DCIClassId) = 8.
I don't want to use __attribute (pack)__ or some thing similar which
comes with gcc. What is a good portable solution for this issue.
Regards
Ninan
Reading various posts on the newsgroups i was under the impression
that memcpy copy the padding bytes for struct. Apparently it does not
do for me. Here is what i am trying to do
typedef struct DCIClassId {
int size;
unsigned char data; /* place holder for variable length data to follow
the struct*/
}DCIClassId;
this structure is followed by a variable no of integers, which is
stored immediatly after the structure. the size field determines the
number of integers
for example no of integers = size - sizeof(DCIClassId). The memory for
the integers to follow is allocated when the memory is allocated for
the structures
DCIClassId *classId;
cidp = (DCIClassId *)malloc (sizeof(DCIClassId) +
no_of_integers*sizeof(int));
I cannot change the struct definition as it is fixed by the standard
for the product i am working on.
My problem is I want to copy this structure into a character array to
be send over socket and retrieve it. I am using memcpy for it.
char sendmesg[4096]
memcpy(sendmesg + MSGID_SIZE , cidp, classId->size);
/* This is code following theoretically should on another process. But
i tested it also on the same process, to make sure it is not a network
issue.
*/
cidp = (DCIClassId *)malloc(classId->size);
memcpy(cidp, sendmesg + MSGID_SIZE, classId->size);
The integers following the struct are left shifted. and I am not able
to retrieve it. I store three integers following the struct. The
second integer is in the place of the first. The third integer is at
the location of second
I am using gcc 3.2
sizeof(DCIClassId) = 8.
I don't want to use __attribute (pack)__ or some thing similar which
comes with gcc. What is a good portable solution for this issue.
Regards
Ninan