Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > memcpy problem with padding for word alignment!!! very urgent

Reply
Thread Tools

memcpy problem with padding for word alignment!!! very urgent

 
 
Ninan Thomas
Guest
Posts: n/a
 
      08-21-2003
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
 
Reply With Quote
 
 
 
 
Paul Pluzhnikov
Guest
Posts: n/a
 
      08-22-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Ninan Thomas) writes:

> 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.


It most certainly does, though this has absolutely nothing to do
with your problem. The padding bytes are *between* the elements
of the struct, not *after* it. What you have is often called
"stretchy array".

> stored immediatly after the structure. the size field determines the
> number of integers


It is rather difficult to tell what you are doing (are you using
bytes at &cidp->data or not ?).

Post a small self-contained example, and your bug will be revealed
to you.

Cheers,

Followup set to comp.unix.programmer, I think this is off topic
everywhere else.
--
In order to understand recursion you must first understand recursion.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      08-22-2003
[Followups set to comp.lang.c - otherwise we'll be in crosspost hell again
for a week.]

Ninan Thomas wrote:

> Hi all
> Reading various posts on the newsgroups i was under the impression
> that memcpy copy the padding bytes for struct.


It does. You appear to have misunderstood what padding bytes are.

> 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


Heh - I can tell what's coming.

>
> 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));


In C, you can lose the cast. (In C++, you can't.) Dennis Ritchie calls your
technique "unwarranted chumminess with the implementation". Note that you
might want to change char data to char data[1].

>
> I cannot change the struct definition as it is fixed by the standard
> for the product i am working on.


Then your product standard is broken (it must be, because the struct
definition is broken!).

>
> 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);


You've left out a rather crucial piece of information here - the code used
to calculate 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.


You seem to want an array of integers, where you get to choose the number of
integers at runtime.

#include <stdlib.h>

#ifdef __cplusplus
#define SC (int *) /* Spurious cast, for C++ */
#else
#define SC
#endif

typedef struct DCIClassId
{
int *data;
} DCIClassId;

DCIClassId dci = {0};

When you're ready to roll, just do this (if you are in C++, put a cast to
(int *) in place of SC; if you're in C, just leave the useless pointless
program-breaking cast out):

dci.data = SC malloc((n + 1) * sizeof *dci.data);
if(dci.data != NULL)
{
dci.data = n;

At this point, stuff the array with some data.

You can now cheerfully memcpy this stuff as a single block, as follows:

#if NINAN_WANTS_THE_SIZE_INCLUDED_IN_THE_COPY
memcpy(target, dci.data, dci.data[0] * sizeof *dci.data);
#else
memcpy(target, dci.data + 1, (dci.data[0] - 1) * sizeof *dci.data);
#endif


--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-22-2003
Paul Pluzhnikov wrote:
> (E-Mail Removed) (Ninan Thomas) writes:
>
>
>>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.

>
>
> It most certainly does, though this has absolutely nothing to do
> with your problem. The padding bytes are *between* the elements
> of the struct, not *after* it. What you have is often called
> "stretchy array".


Padding bytes may and often do come at the end of a struct. This is
necessary to ensure proper alignment for the *first* element of the
struct when you create an array of structs.

It sounds like the OP is using some bizarre variant of the "struct
hack", which relies on undefined behavior and I think is a very bad idea.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need help doing some question very very urgent plzzz vhdl124 VHDL 1 04-28-2008 01:06 AM
very very urgent ahmdsd_ostora Hardware 2 05-07-2007 12:38 PM
Very Very Urgent - Please help me settyv@gmail.com ASP .Net 3 11-15-2006 09:02 PM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
memcpy problem with padding for word alignment!!! very urgent Ninan Thomas C++ 3 08-22-2003 06:19 AM



Advertisments