code crashes on Solaris

This is a discussion on code crashes on Solaris within the C++ Programming forums, part of the General Programming Boards category; This code crashes when Sun CC is used to compile on Solaris platform. Anybody knows why? The place where the ...

  1. #1
    watcher
    Guest

    Question 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;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    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

  3. #3
    watcher
    Guest

    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.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  3. ASM 'Out Instruction' crashes, code included.
    By Xei in forum C Programming
    Replies: 5
    Last Post: 03-03-2003, 04:42 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21