Is there a safer way to typecast data from a generic pointer.? More specifically , is there a way to check if the type casting is safe or not.
Suppose void*data we receive from the recv function in netwrking code.
Suppose there are two structures:
struct data1
{
int val;
double val1;
}
struct data2
{
char str[100];
long double val3;
}
Suppose we have the following receive call:
recv(int s, void *buf, size_t len, int flags);
and buf can be of type struct data1 or struct data2. Here are my questions:
Can we perform some sort of check of which type is stored in buf?
What happens if the following happens:
// buf contains a message of type data1 struct data2 *d2; // assume its malloced too d2 = (struct data2)buf; d2->val3=3.145