Note that there are some explanatory texts on larger screens.

plurals
  1. POmemcpy copying the wrong number of bytes
    primarykey
    data
    text
    <p>1.I have the following structure.</p> <pre><code>typedef struct { unsigned int ibaseRecord; unsigned int irecordNumber; unsigned char brecordType; unsigned char brevision; unsigned int ipageNumber; unsigned int ireserve1; unsigned int ireserve2; unsigned int ireserve3; unsigned short scrc16; } DATABASEPAGEHEADER_TypeDef; </code></pre> <p>I also have following byte buffer storing an array.</p> <pre><code>char msg_response[]={0x9A,0x17,0x00,0x00,0x17,0x00,0x00,0x00,0x04,0x02,0x9F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x23,0xEA,0x4A,0x86}; </code></pre> <p>Using memcpy, I expect to map the buffer into the structure.</p> <pre><code> DATABASEPAGEHEADER_TypeDef * varPageHeader; varPageHeader=(DATABASEPAGEHEADER_TypeDef*)malloc(sizeof(DATABASEPAGEHEADER_TypeDef)); memcpy(varPageHeader,msg_response,sizeof(DATABASEPAGEHEADER_TypeDef)); </code></pre> <p>However, the result messes up. Here is the inspection I made using gdb (explore the struct). Things were fine until the ipageNumber, which I expected to have value 0x0000009f.And also later in ireserveX (x=1,2,3).</p> <pre><code>ibaseRecord = 0x179a .. (Value of type 'unsigned int') irecordNumber = 0x17 .. (Value of type 'unsigned int') brecordType = 0x4 .. (Value of type 'unsigned char') brevision = 0x2 .. (Value of type 'unsigned char') ipageNumber = 0x0 .. (Value of type 'unsigned int') ireserve1 = 0x0 .. (Value of type 'unsigned int') ireserve2 = 0x0 .. (Value of type 'unsigned int') ireserve3 = 0xea230000 .. (Value of type 'unsigned int') scrc16 = 0x864a .. (Value of type 'short unsigned int') (gdb) print sizeof(unsigned int) $7 = 0x4 </code></pre> <p>Does anyone have solution to it?</p> <p>problem solved. It was the struct alignment problem. Thank you for your kind help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload