Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] Structure Copying Issue in Keil

Status
Not open for further replies.

tdmicro

Newbie level 1
Joined
Sep 3, 2019
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
13
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
 

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
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top