Issues with memcpy

Christian Convey
Wed Apr 27 16:23:00 GMT 2005

At least in past days, g++ supported a "packed" keyword.  If you really, really 
want the struct to occupy exactly 9 bytes, this could be a way to accomplish that.

I generally wouldn't worry about that for performance reasons, though, as that 
wouldn't reduce the nubmer of *words* of data that get copied.

The only good reasons I can think of for using the "packed" structure are:
- You're using the struct to overlay a buffer that's used in network
   communications, so you want exactl control over which bytes of the buffer
   correspond to particular members of the struct, or

- You're going to have a huge number of instances of this struct in memory, and
   you really need that 25% savings in memory use.  (Although if you allocate
   heap memory for individually for each instance of the struct, you still might
   not get any savings in heap use.  IIRC, the heap is likely to allocate a
   bigger chunk of memory than just 9 bytes, when you ask for a 9-byte

Brian Budge wrote:
> You could reorganize your struct so that the char is on the end.  Then
> you could just do the memcpy with 9 bytes.  However, I have a feeling
> that doing the full 12 byte copy may be faster... like others have
> said, it depends on your achitecture.
>   Brian
> On 4/27/05, Javier Valencia <> wrote:
>>Thanks John, it was the problem, uninitialized struct.
>>To all: i understand now whats happening about aligned stuff, thanks!!
>>So what is the best way to copy a struct to a byte array without copying
>>empty aligned spaces?
>>I'm doing now the ugly way, just pointing to each field each time and
>>copying to the buffer.
>>It's for transmition purposes.

Christian Convey
Computer Scientist,
Naval Undersea Warfare Center
Newport, RI

More information about the Gcc-help mailing list