Wrong data copied to struct from void pointer

codex10

Ok so I've run out of things to try and fix this.

I've got a struct as follows:

typedef struct
{
   u8int  NodeType;
   u32int Group;
   u32int Direction;
   u16int ID;

} tsTargetNode;


void vTrackNode(const uint8 *pu8Val, uint8 u8Len, void *pvCbData)
{
   /* Copy data */
   memcpy(pvCbData, pu8Val, u8Len);

   /* Doing this gives the wrong data */
   //tsTargetNode *node = (tsTargetNode *) pvCbData;  

   /* Doing this gives the right data */
   memcpy(&tsTargetNode.NodeType, pvCbData, 1);
   memcpy(&tsTargetNode.Group, pvCbData+1, 4);
   memcpy(&tsTargetNode.Direction, pvCbData+1+4, 4);
   memcpy(&tsTargetNode.ID, pvCbData+1+4+4, 2);
}

For example: Data passed *pu8Val = 0x AA BB CC DD EE FF 11 22 33 44 55 66 77 88 99 00

using pointer, the data I retrieved is:

NodeType  = 0xAA
Group     = 0xFF112233
Direction = 0x44556677
ID        = 0x8899 

but manually memcpy, the data I retrieved is correct

NodeType  = 0xAA  
Group     = 0xBBCCDDEE
Direction = 0xFF112233
ID        = 0x4455

it seems like the first set it tries to copy 4 bytes into the NodeType.

Am I doing the pointer copy wrong? or could this be a device / HW problem?

Useless

The compiler is free to add padding between the members of a structure, so that each member is aligned to whatever is natural for your platform - here, it looks like 32 bits. Try printing sizeof(tsTargetNode), and dumping that many bytes from an initialized tsTargetNode to see how the fields are laid out.

You haven't said what compiler you're using, but there will be a compiler-specific way to disable padding for your structure. If you do that, its memory layout will match the buffer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wrong data copied to struct from void pointer

From Dev

Why struct is giving wrong sizeof and void pointer

From Dev

How to initialize an void* pointer from struct

From Dev

How to initialize an void* pointer from struct

From Dev

Void pointer to a struct

From Dev

void pointer : what is wrong?

From Dev

Casting a void pointer to a struct and initialise it

From Dev

Pointer to function that was copied from code

From Dev

Pointer to function that was copied from code

From Dev

c - void pointer to struct inside a struct

From Dev

Pointer on struct can not be copied to another pointer inside a function

From Dev

How to access data from pointer in struct from Python with ctypes?

From Dev

type casting void pointer to struct type

From Dev

Cython: How to expose void* and function pointer in struct?

From Dev

void pointer to struct, element access using macros

From Dev

Cython: How to expose void* and function pointer in struct?

From Dev

The wrong data is being copied into another sheet

From Dev

C++11 std::promise returning std::string from thread, data pointer looks copied not moved

From Dev

C++11 std::promise returning std::string from thread, data pointer looks copied not moved

From Dev

Read from file and put data into dynamic struct using pointer

From Dev

Wrong pointer after copying all data from array to a structure

From Dev

How to correctly cast void pointer to blob to a struct-pointer?

From Dev

in C, what's the deal of casting pointer to pointer to struct ao void*

From Dev

Wrong output when printing a string from inside a struct pointer (VC++ 2010)

From Dev

Pointer Truncation from void* to DWORD

From Dev

Want to access struct pointer's data of type from struct's memory in C

From Dev

PInvoke a struct pointer to get the data

From Dev

Accessing struct from double pointer

From Dev

Accessing struct from double pointer

Related Related

  1. 1

    Wrong data copied to struct from void pointer

  2. 2

    Why struct is giving wrong sizeof and void pointer

  3. 3

    How to initialize an void* pointer from struct

  4. 4

    How to initialize an void* pointer from struct

  5. 5

    Void pointer to a struct

  6. 6

    void pointer : what is wrong?

  7. 7

    Casting a void pointer to a struct and initialise it

  8. 8

    Pointer to function that was copied from code

  9. 9

    Pointer to function that was copied from code

  10. 10

    c - void pointer to struct inside a struct

  11. 11

    Pointer on struct can not be copied to another pointer inside a function

  12. 12

    How to access data from pointer in struct from Python with ctypes?

  13. 13

    type casting void pointer to struct type

  14. 14

    Cython: How to expose void* and function pointer in struct?

  15. 15

    void pointer to struct, element access using macros

  16. 16

    Cython: How to expose void* and function pointer in struct?

  17. 17

    The wrong data is being copied into another sheet

  18. 18

    C++11 std::promise returning std::string from thread, data pointer looks copied not moved

  19. 19

    C++11 std::promise returning std::string from thread, data pointer looks copied not moved

  20. 20

    Read from file and put data into dynamic struct using pointer

  21. 21

    Wrong pointer after copying all data from array to a structure

  22. 22

    How to correctly cast void pointer to blob to a struct-pointer?

  23. 23

    in C, what's the deal of casting pointer to pointer to struct ao void*

  24. 24

    Wrong output when printing a string from inside a struct pointer (VC++ 2010)

  25. 25

    Pointer Truncation from void* to DWORD

  26. 26

    Want to access struct pointer's data of type from struct's memory in C

  27. 27

    PInvoke a struct pointer to get the data

  28. 28

    Accessing struct from double pointer

  29. 29

    Accessing struct from double pointer

HotTag

Archive