C. To segmentation fault or not to segmentation fault, that is the question.


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 1 Week Ago
I've seen similar things in lots of code ported to different architectures. Quite a few different gotchas where something you can ignore harmlessly on one system, explodes on another, because something you took for granted has changed.

If you forget to include stdlib.h and math.h, your code may still work in 32-bit. It will assume these undeclared functions take all-integers and fill in the blanks. On a 64-bit system, where pointers are twice as large, that assumption is catastrophically wrong and causes memory addresses to get mangled, causing a crash.

Also, some systems let you do this, but don't depend on it, because most don't:
Code:
char *stuff="abcdefg";
stuff[2]='Q';

This User Gave Thanks to Corona688 For This Post:
wisecracker (1 Week Ago)
# 9  
Old 1 Week Ago
Code that has problems like that is NOT working, despite your personal point of view. It is undefined behavior. So you understand: undefined behavior means the program is no longer following any rules. It went into lala land. It could limp forward until it hits an exit point, it could format all disks, it could contact the New Horizons spacecraft and order a pizza.

I would say the compiler implementers for the box's C compiler code decided to do some goof-proofing. The limp along option. They likely have a SIGSEGV signal trap? I do not know. If you have a truss equivalent on the box, you can find out if the segmentation fault has been blocked somehow.

I am not trying to be a grumpy old problem monger. But undefined behavior is never "working". Ask Don Cragun or Corona688.
These 3 Users Gave Thanks to jim mcnamara For This Post:
Corona688 (1 Week Ago) Don Cragun (3 Days Ago) wisecracker (1 Week Ago)
# 10  
Old 1 Week Ago
Another way that looks nicer and should still work on older compilers:

Code:
int main(int argc, char *argv[])
{
    if (argc <= 3)
    {
        printf("ERROR!\n\n");
        printf("Not enough arguments!\n");
        exit(1);
    }

    /* Older compilers only let you put variables at the top of a code block. */
    /* But you can put code blocks wherever you want. */
    {
        int NUM_1 = strtod(argv[1], NULL);
        int NUM_2 = strtod(argv[3], NULL);
        CHARACTER = *argv[2];
        printf("\n%i, %i, %c\n\n", NUM_1, NUM_2, CHARACTER);
    }

    return(0);
}

This User Gave Thanks to Corona688 For This Post:
wisecracker (1 Week Ago)
# 11  
Old 1 Week Ago
Hi all...

All cleaned up and working in AMIGA mode too now.
Thanks C688 for the code snippet.

I honestly thought that setting the parameters globally would suffice but it turns out I am wrong.

Thanks all for your input.
This embarrassment I will never forget.
# 12  
Old 3 Days Ago
Don't be embarassed! C is very different from most languages. It's a high-level language which writes pure assembly, with all the safety you get from pure assembly...
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Segmentation fault sizzler786 Programming 2 09-30-2013 07:29 AM
Segmentation Fault femchi Homework & Coursework Questions 3 11-30-2012 10:59 PM
Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault pooyair Programming 6 10-13-2012 04:17 AM
Segmentation fault in C omega666 Programming 7 03-30-2011 04:06 PM
Segmentation fault. sepoto Programming 1 12-19-2010 10:07 PM
segmentation fault gokult Programming 1 12-10-2010 10:47 AM
segmentation fault mind@work Programming 3 07-07-2010 01:38 PM
segmentation fault joey Programming 3 12-22-2008 04:28 PM
Segmentation Fault monika UNIX for Dummies Questions & Answers 5 05-30-2008 04:30 AM
Segmentation fault big123456 Linux 0 07-20-2007 05:01 AM
Segmentation fault rshaikh AIX 2 04-16-2007 08:12 AM
segmentation fault rockgal Programming 8 12-05-2006 11:16 AM
Hi! segmentation fault vijlak Programming 4 11-13-2006 09:48 AM
Segmentation Fault compbug UNIX for Dummies Questions & Answers 3 04-21-2006 10:43 AM
segmentation fault wojtyla Programming 3 02-19-2005 01:53 PM