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

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 8  
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:
char *stuff="abcdefg";

This User Gave Thanks to Corona688 For This Post:
# 9  
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:
# 10  
Another way that looks nicer and should still work on older compilers:

int main(int argc, char *argv[])
    if (argc <= 3)
        printf("Not enough arguments!\n");

    /* 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);


This User Gave Thanks to Corona688 For This Post:
# 11  
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  
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 for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #368
Difficulty: Medium
The Open Group released the Single UNIX Specification Version 2 in 1987.
True or False?

10 More Discussions You Might Find Interesting

1. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

2. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

3. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

4. Programming

segmentation fault

What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies

5. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

6. Programming

segmentation fault

ive written my code in C for implementation of a simple lexical analyser using singly linked list hence am making use of dynamic allocation,but when run in linux it gives a segmentation fault is it cause of the malloc function that ive made use of????any suggestions as to what i could do??? thank... (8 Replies)
Discussion started by: rockgal
8 Replies

7. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

8. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies

9. AIX

Segmentation fault

I am tring to install Lotus Domino/Notes 5.0.5 on a AIX 4.3.3 server. I go to run the cdrom/ibmpow/install and I get the following error. Lotus Notes for Unix Install Program --------------------------------------------- ./install: 10088 Segmentation fault This had Lotus Notes installed... (1 Reply)
Discussion started by: jshaulis
1 Replies

10. Programming

segmentation fault

hi all i'm trying to execute a c program under linux RH and it gives me segmentation fault, this program was running under unix at&t anybody kow what the problem could be? thanx in advance regards (2 Replies)
Discussion started by: omran
2 Replies

Featured Tech Videos