+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Newbie level 1
    Points: 19, Level: 1

    Join Date
    Sep 2019
    Posts
    1
    Helped
    0 / 0
    Points
    19
    Level
    1

    Structure Copying Issue in Keil

    hi,
    we are trying to copy some data in a structure (IDE :keil , lang - cpp)
    structure
    Code:
    typedef unsigned char U8;
    typedef struct 
    {
    	U8 pD;
    	U8 sD;
    	U8 dD;
    	U8* txdata;
    }data_ID;
    typedef struct 
    {
    	sObj Data;
    	unsigned char ms;
    	unsigned char status;
    }zmtData;
    
    
     typedef struct //autogenerated structure 
     {
         ubyte cG;    
         ulong uD;       
         ulong uk;     
         ubyte ubData[8];  
         uword cR;  
     }sObj;
    and the copying function is
    Code:
    Len=4;
     sObj rxData;
    	data_ID *dataID = (data_ID *)malloc(sizeof(U8)*(Len+3));// Allocate memory for struct data_ID
    	dataID->txdata = (U8*)malloc(sizeof(U8)*Len);
    	read_data(&rxData);
     /*
     rxData.ubData value is 7,8,9,10,11,12,13,14
     */
    	memcpy(dataID,rxData.ubData, sizeof(data_ID));//dataID=(data_ID*)rxData.ubData;
    rxData.ubData[] value is 7,8,9,10,11,12,13,14
    while copying from rxData.ubData[] to dataID, in watch window, we can see the following details
    dataID-> pD= 7;
    dataID-> sD= 8;
    dataID-> dD= 9;
    dataID-> txdata= 00 13 12 11;
    but expected value is
    dataID. txdata= 13 12 11 10;
    while debuging the code, 3rd index value of rxData.ubData 10 is missing. so we thought it is because of structural padding and used #pragma pack(1)
    then we are getting exception (classB_trap)

    kindly help us to find out the mistake in the above code

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 5
    Points: 9,192, Level: 22
    Achievements:
    7 years registered
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,600
    Helped
    365 / 365
    Points
    9,192
    Level
    22

    Re: Structure Copying Issue in Keil

    You have no understanding of pointers
    Code:
    data_ID *dataID = (data_ID *)malloc(sizeof(data_ID ));// Allocate memory for struct data_ID
    After this txdata will still be a undefined pointer and have to be allocated next
    Love me or hate me. All infractions will be ignored.



--[[ ]]--