-
code crashes on Solaris
This code crashes when Sun CC is used to compile on Solaris platform. Anybody knows why? The place where the crash occurs is,
float aa = a->k;
Thanks.
Code:
typedef struct {
int i;
int j;
char c[13];
float k;
} TRY;
void donth()
{
int i;
int k;
char tempp;
char tmp[100];
TRY* a = (TRY *)tmp;
float aa = a->k;
}
int main()
{
donth();
return 0;
}
-
I'll put my money on Bus Error due to un-aligned memory access.
Hopefully, you're doing this just to understand the "why".
There are several issues to consider when reading that code:
(1) platform alignment requirments for accessing a float - may need to be on a 4, 8, 32, etc. byte boundry
(2) compiler's structure padding - how many bytes did the compiler pad after c[13]? Probably enough to access k on the correct boundry (see 1.)
(3) on what boundary did the compiler put tmp[100] on the stack? Apperantly not the same boundry had you declared a struct TRY in tmp's place.
gg
-
bus error
Codeplug,
You are right. It's a bus error. The interesting thing is that
I've tested this code on different platforms using different
compilers. Looks like it's compiler-related. Only Sun CC has
such kind of problem. Does this mean we should be very
careful when dealing with Sun CC compiler on Solaris
platform? ;)
BTW, the code was just for this test.
Thanks and have a good night.
-
You can't just say it's compiler related: you said yourself, "I've tested this on different platforms", see (1). It could also be the compiler, see (2), but that doesn't mean Sun CC is doing something wrong. Should you be "very carefull"? Well, you shouldn't be writting code like that that at all. If you absolutely need two different types to occupy the same memory, then use a union or figure some other standard/portable method to do what you want.
gg