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.
PHP Code:
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); }
};