Porting to g++ from MS VC++ - question
Artem Alimarine
aalimari@best.ms.philips.com
Fri Oct 1 00:00:00 GMT 1999
Hello David,
I do not mention that relying on a struct format is usually not a good practice,
because different compilers use different paddings, long can be 32 bit on one and
64 on the other, and of course little-endian and big-endian problems. The problem
you have is an example of different format of the structures/classes generated by
different compilers.
Even if you make it work, the program will be not easily maintainanle and portable.
Every time new release of a compiler comes, you will not be sure it works. If you
replace linux pc with, let's say sun you get endianness problems (order of bytes in
ints is different).
But if you still want it ti send structures as it is, you better use the same
structure on send and side and on receive side
a workaround
#include <stdio.h>
class test_class
{
public:
void print_test(void) { printf("TEST\n"); }
virtual ~test_class() {}
};
struct I : public test_class {
int j[2];
virtual ~I() {}
};
struct J
{
int k[2];
virtual ~J() {}
};
main()
{
I i;
J j;
printf("sizeof i = %d\n", sizeof(i));
printf("sizeof j = %d\n", sizeof(j));
}
The size of the structures will be the same. But you have to be sure that you do
not destroy the vmt pointer when you receive such a class.
Normally, this is done using serialization, when each class knows how to send and
receive itself.
I hope it is of any help
Artem
More information about the Gcc-help
mailing list