Thread: Memory allocation problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Memory allocation problem

    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 dataunsigned long length)
        {        
    memcpy(mData.mMessagedatalength);    }    

    }; 

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Maybe you meant length + 1 if you are working with c-style strings.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM