PDA

View Full Version : Help on memcpy


machado
11-04-2005, 07:15 AM
Please help me how to use memcpy in the following situation.
When I run the following code, I am getting Segmentation error due to memcpy. I can see some problem with my Setdata function. how do I correct it.

struct Packet_t
{
int mType;
int mUnit;
unsigned long mLength;
char mMessage[1];
} __attribute__((__packed__)); // packet is packed...


class PacketAccessor
{
private:

Packet_t mData;

public:
//Default Constructor
PacketAccessor(): mData()
{
}

//Default Destructor
~DistributionPacketAccessor()
{

}

//--------------------------------------------------------

//! Get accessor functions
//--------------------------------------------------------

int GetType() const { return mData.mType; };
int GetUnit() const { return mData.mUnit; };
unsigned long GetLength() const { return ntohs(mData.mLength); };
char *GetData() { return mData.mMessage;}

//--------------------------------------------------------
//! Set accessor functions
//--------------------------------------------------------
void SetTypeIdentifier(const int type) { mData.mType = type; };
void SetUnit(const int unit) { mData.mUnit = unit; };
void SetLength(const int dataLength) { mData.mLength = htons(dataLength); }; void SetData(char * data, unsigned long length)
{ memcpy(mData.mMessage, data, length); }

};

_Dan
11-04-2005, 11:07 AM
How do you use the Setdata function?
Remember "char mMessage[1];" has only room for 1 byte, so length must always be 1 (or 0...)