Thread: How to give an "emergency exit" to my program?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    2

    Question How to give an "emergency exit" to my program?

    Hi,

    I am working on a small program project and I often need to open files and use memory allocation, like this:

    FILE* fastaFile;
    if ((fastaFile = fopen(fastaFileName, "w")) == NULL)
    {
    printf("Error opening file %s.\n", fastaFileName);
    printf("ERROR_INVALID_fastaFileName");
    }

    or

    char* theLine;
    if ((theLine = (char*)calloc(MAXLINELENGTH, sizeof(char))) == NULL)
    {
    printf("Error of memory allocation.\n");
    printf("ERROR_getEnzyme_theLine\n");
    }
    theLine[0] = '\0';


    The problem is that while the error message will print, it doesn't change the fact that the program crash a few lines later (since a call is made to these values). So the program doesn't quit properly. Is there is a way to print an error message and ask to quit the program, or do I need Windows (or any OS) to tell me the program did something wrong every time?

    Thanks for your help.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, the easiest way is to use return codes. Just check the return value in the calling function, and do the appropriate thing.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use the return statement to leave a function, this is the graceful way.

    Use the exit() call to exit your program, but this isn't graceful.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *fp;
        
        if ((fp = fopen("file.txt", "r")) == NULL)
        {
            perror("file.txt");
            return (EXIT_FAILURE);
        }
        printf ("File opened successfully\n");
        
        fclose(fp);
        
        return (EXIT_SUCCESS);
    }
    If you're going to return from a function, make sure you return something that denotes the failure. Likes this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int DoIt(FILE *fp);
    
    int main(void)
    {
        FILE *fp;
        
        if ((fp = fopen("file.txt", "r")) == NULL)
        {
            perror("file.txt");
            return (EXIT_FAILURE);
        }
        printf ("File opened successfully\n");
        
        if (DoIt(fp) < 0)
            printf ("Error in function do it\n");
            
        fclose(fp);
        
        return (EXIT_SUCCESS);
    }
    
    int DoIt(FILE *fp)
    {
        char buf[BUFSIZ];
        if (fgets(buf, sizeof buf, fp) == NULL)
            return -1;
    
        printf ("buf is %s", buf);
        
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    2
    Thanks, I think return will be the best way to do this.

    But I still have one question about it.

    When an error of memory allocation happens, it basically means that the memory is "saturated".
    After the return statement, my program will return to it's prompt. Should I include a suggestion to quit the program (or free memory), in the error message, or is it safe to continue knowing that the memory is somehow messed up?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Another way to have your program terminate immediately is to use uninitialized pointers. Something like:
    Code:
    void terminate( void )
    {
        char **quit;
        int x;
        for( x = 0; x < 10000; x++ )
            strcpy( quit[x], "quit now please" );
    }
    ...will usually do the trick for you.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a controlled way to run out of memory. I haven't run it in full, but it should work.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char *p = NULL, *tmp;
        long int l = 0;
        
        while ((tmp = realloc(p, 4096)) != NULL)
        {
            p = tmp;
            l++;
        }
        
        printf ("We ran out of memory after %ld loops\n", l);
        
        free(p);
        
        return (EXIT_SUCCESS);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a controlled way to run out of memory. I haven't run it in full, but it should work.
    I wasn't trying to run out of memory, only showing how to make your program perform an "emergency exit".

    Actually, depending on what OS you're running, I don't think your program will actually run out of memory--or at least, it won't untill your hard drive is full!

    I remember running something similar before in ... WinME? Win98? Something. ... and it just kept filling my hard drive. Kinda nifty.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>>I wasn't trying to run out of memory
    Yeah, I could see what you were doing I was (kind of) answering the question in the previous post.

    >>>Kinda nifty.
    That made me laugh!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Call a program & still capture cmds
    By Zuconta in forum C Programming
    Replies: 3
    Last Post: 07-23-2003, 03:54 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM