Struct has different size after being passed to func

joshwl2003

I have created a structure with a Id variable and a list of values. When I check the size before passing it to a function as a void* it is 12. Once it gets in the function it is 4. How do I keep the size the same?

Struct Code:

typedef struct
{
    int id;
    std::list<int> sensorValues;
}dataPacketDef;

Sending to other function code:

  void SendDataBufferToServer()
{
    cout << "Sending network data size: " << sizeof(dpd) << "\n";
    client.writeData((void*)&dpd);
}

In the above function the size is 12 but in the receiving function the size is only 4. The struct is declared as a global variable for my main program.

dlf

The size of a pointer is 4 bytes (or 8 if you were using an x64 compiler). If writeData needs to know the size of the pointed-to object, that information will need to be passed to it somehow; a common idiom is to pass the size as a second argument. With that done, you could use a helper function template to simplify things:

template<typename T>
void writeData(Client& client, const T& obj)
{
   client.writeData((void*)&obj, sizeof(obj));
}

Although as molbdnilo noted in the question comments, client.writeData() probably still won't be able to do what you want it to due to the presence of a list in dataPacketDef.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Struct has different size after being passed to func

From Dev

Struct has different size if the field order is different

From Dev

C++: Pointer contains different address after being passed

From Dev

Displaying data from Struct being passed around

From Dev

Packed struct has not the expected size

From Dev

ArrayList that has Rectangles, is being passed as an Object

From Dev

invalid size of struct after realloc

From Dev

Can Windows filesystem files show different size after being copied on a linux filesystem?

From Dev

Different address after struct initialization

From Dev

Compare two different size vectors of the same struct

From Dev

Compare two different size vectors of the same struct

From Dev

Join two different size cells in the same struct

From Dev

UICollectionViewCells are resetting their size after being rearranged

From Dev

Tabhost + viewPager + ListView; Different value being passed in bundle android

From Dev

C pointer does not get set after being passed to a function

From Dev

Changing a Labels value in Javascript after being passed into a function

From Dev

Parameter object becomes null after being passed to function

From Dev

UITextField placeholder has different font size

From Dev

Why is the size of an array different when it's passed as a parameter in a function

From Java

Parsed byte array turns into garbage after being parsed into a struct

From Dev

Struct Array Indexes not being filled after first index

From Dev

Pointer value set to zero after being changed in a struct method

From Dev

Queue struct losing element values after being called once

From Dev

Ruby - Modify Struct attribute immediately after it's being initialized

From Dev

After upgrade to Sitecore 6.6, thumbnail image size parameters are inappropriately passed

From Dev

Why is BigInteger in C# a struct if it has an unbounded size?

From Dev

Canvas widget has wrong size after resize

From Dev

Maximize size of Silverlight content after it has loaded

From Dev

Run code after some time has passed or a condition is met

Related Related

  1. 1

    Struct has different size after being passed to func

  2. 2

    Struct has different size if the field order is different

  3. 3

    C++: Pointer contains different address after being passed

  4. 4

    Displaying data from Struct being passed around

  5. 5

    Packed struct has not the expected size

  6. 6

    ArrayList that has Rectangles, is being passed as an Object

  7. 7

    invalid size of struct after realloc

  8. 8

    Can Windows filesystem files show different size after being copied on a linux filesystem?

  9. 9

    Different address after struct initialization

  10. 10

    Compare two different size vectors of the same struct

  11. 11

    Compare two different size vectors of the same struct

  12. 12

    Join two different size cells in the same struct

  13. 13

    UICollectionViewCells are resetting their size after being rearranged

  14. 14

    Tabhost + viewPager + ListView; Different value being passed in bundle android

  15. 15

    C pointer does not get set after being passed to a function

  16. 16

    Changing a Labels value in Javascript after being passed into a function

  17. 17

    Parameter object becomes null after being passed to function

  18. 18

    UITextField placeholder has different font size

  19. 19

    Why is the size of an array different when it's passed as a parameter in a function

  20. 20

    Parsed byte array turns into garbage after being parsed into a struct

  21. 21

    Struct Array Indexes not being filled after first index

  22. 22

    Pointer value set to zero after being changed in a struct method

  23. 23

    Queue struct losing element values after being called once

  24. 24

    Ruby - Modify Struct attribute immediately after it's being initialized

  25. 25

    After upgrade to Sitecore 6.6, thumbnail image size parameters are inappropriately passed

  26. 26

    Why is BigInteger in C# a struct if it has an unbounded size?

  27. 27

    Canvas widget has wrong size after resize

  28. 28

    Maximize size of Silverlight content after it has loaded

  29. 29

    Run code after some time has passed or a condition is met

HotTag

Archive