Can anyone confirm this?
I am receiving data over the network and cast it to a struct, for example.
My program randomly crashes or generates a datatype misalignment error when doing network operations. I am pretty sure it is because I am accessing a dword when data1 is not dword aligned in memory (because of the cast from networkdata which is a byte*).
So in order to fix this I am going to have to do the following:
I just wanted to double check if anyone else has seen this type of problem before I go and modify all of my receive data code to try to fix this issue.
I am receiving data over the network and cast it to a struct, for example.
- Code:
struct packet
{
dword data1;
word data2;
};
void receivedata(byte *networkdata, int size)
{
packet *data = (packet*)networkdata;
data->data1; <-- datatype misalignment
}
My program randomly crashes or generates a datatype misalignment error when doing network operations. I am pretty sure it is because I am accessing a dword when data1 is not dword aligned in memory (because of the cast from networkdata which is a byte*).
So in order to fix this I am going to have to do the following:
- Code:
void receivedata(byte *networkdata, int size)
{
packet data;
memcpy(&data, networkdata, sizeof(data));
data.data1; <-- no misalignment anymore because its on the stack
}
I just wanted to double check if anyone else has seen this type of problem before I go and modify all of my receive data code to try to fix this issue.