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.
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.
Comments