We often write large and complex programs. We allocate memory, open files/ resources and lots of other things. Most of the time our program executes in proper environment and encounters less/no error. We should handle this errors and exit properly.

Exiting from a logic or program involves freeing up memories and closing handles and files. We often write a cleanup handler which does this work. once cleanup is done we can exit gracefully. This cleanup ensures no memory and resource leak.

C runtime provides a atexit() API to register for cleanup handlers.

int atexit(void (*function)(void));

User need to implement these callbacks and C runtime will call all registered callbacks in reversed order. Maximum 32 routines can be registered for one application.

#include <stdlib.h>
#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<unistd.h>
FILE* fp = NULL;
char *hostname = NULL;
10 
11  void free_memory(void)
12  {
13    printf ("Cleaning memory\n");
14    if (hostname) {
15      free (hostname);
16    }
17  }
18 
19  void close_files(void)
20  {
21    printf ("Closing files\n");
22    if (fp) {
23      fclose (fp);
24    }
25  }
26 
27  int main(int argc, char* argv[])
28  {
29    int err;
30    err = atexit (free_memory);
31    if (err != 0) {
32      fprintf (stderr, "cannot set exit function\n");
33      exit(-1);
34    }
35    err = atexit (close_files);
36    if (err != 0) {
37      fprintf(stderr, "cannot set exit function\n");
38      exit(-1);
39    }
40    hostname = (char *) malloc(HOST_NAME_MAX);
41    if (hostname == NULL) {
42      fprintf(stderr, "Error in memory allocation, exiting.\n");
43      exit(-1);
44    }
45    err = gethostname (hostname, HOST_NAME_MAX);
46    if (err != 0) {
47      fprintf(stderr, "Error %d executing gethostname(), exiting.\n", err);
48      exit(-1);
49    }
50    fp = fopen ("hostinfo.txt", "w");
51    if (fp == NULL) {
52      fprintf (stderr, "Error writing to file, exiting.\n");
53      exit (-1);
54    }
55    fprintf (fp, "Hostname : %s\r\n", hostname);
56    /* try to open a non-existent file */ 
57    if (fopen ("foo", "r") == NULL) {
58      fprintf (stderr, "Oops some error happened, exiting!\n");
59      exit(-1);
60    }
61      /* additional lines here to write more info */
62    /* ........................................ */
63    /* Finally close the file */
64    fclose(fp);
65    free (hostname);
66 
67  }
68 
69  Output:
70  Oops some error happened, exiting!
71  Closing files
72  Cleaning memory
73 
74 

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

argc and argv, C startup routine, argv environ getenv, system(), atexit, raise signal, abort exit,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan