Error Handling Signals Rudra Dutta CSC 230 Spring

  • Slides: 8
Download presentation
Error Handling, Signals Rudra Dutta CSC 230 - Spring 2007, Section 001 Copyright Rudra

Error Handling, Signals Rudra Dutta CSC 230 - Spring 2007, Section 001 Copyright Rudra Dutta, NCSU, Spring, 2007

errno. h l Contains a global integer variable called errno Common variable used by

errno. h l Contains a global integer variable called errno Common variable used by many library routines to indicate errors – Specifications of function (man page) specify what values of errno indicate what errors – l Why not return through return value? Sometimes function semantics require returning various possible values - almost any value may be returned – Different types of error cannot be distinguished between – Solution: return a unique value to indicate error, then set errno to indicate nature of error – l Accompanied by perror and strerror – Print an implementation defined error message for errno – Return a pointer to a string that is the error message Copyright Rudra Dutta, NCSU, Spring, 2007 2

Binary Files l Disk files may have arbitrary structure Text files: those that have

Binary Files l Disk files may have arbitrary structure Text files: those that have line-oriented structures (and are expected to contain text) – Can use shell tools like cat etc. to manipulate – Binay file: no such structure – l Open with an additional b in the second argument of fopen() – l Not different under UNIX, but for portability Random Access functions: fseek: set to particular position in stream – ftell: return current file pointer position – rewind: fseek to beginning –. . . – l Error functions: clearerr: clears error indicators for a particular stream – feof: returns non-zero if end of file indicator is set for stream – ferror: returns non-zero if error indicator for stream is set – Copyright Rudra Dutta, NCSU, Spring, 2007 3

Function Pointers l Functions can be pointed to, just like variables int func 1

Function Pointers l Functions can be pointed to, just like variables int func 1 (int i) { printf ("%dn", i); } int func 2 (short i) { printf ("The value is %dn", i); } int main () { int (*fp) (int); fp fp = func 1; (4); = func 2; (4); } Copyright Rudra Dutta, NCSU, Spring, 2007 4

Complicated Declarations l char **copy – l int (*list)[10] – l Pointer to function

Complicated Declarations l char **copy – l int (*list)[10] – l Pointer to function returning void char (*(*x())[])() – l Function returning pointer to void (*comp)() – l Array of pointers to int void *comp() – l Pointer to array of ints int *list[10] – l Pointer to pointer to char Function returning pointer to array of pointers to functions returning char (*(*x[3])())[5] – Array[3] of pointer to function returning pointer to array[5] of char Copyright Rudra Dutta, NCSU, Spring, 2007 5

signal. h Exception handling l The function signal() is used to “register” an exception

signal. h Exception handling l The function signal() is used to “register” an exception handler for various signals (“exceptions”) l l void (*signal(int sig, void (*func)(int)))(int); After this invocation, a signal will cause the function pointed to by func to be called l sig can be SIGFPE, SIGSEGV, SIGTERM, etc. l – If sig occurs, it will be as if (*func) (sig) was called “De-registers” signal handler - can be re-registered in body of handler – Initial state is system-dependent – l Accompanied by raise () Copyright Rudra Dutta, NCSU, Spring, 2007 6

Bit Manipulation & bitwise and | bitwise or ^ bitwise xor, also called exclusive

Bit Manipulation & bitwise and | bitwise or ^ bitwise xor, also called exclusive or << left shift >> right shift ~ one's complement l Example int i=31, j=226; – i & j returns 2, i|j returns 255, i^j returns 253 – i << 2 returns 124, j >> 3 returns 28 – ~i returns -32 – Copyright Rudra Dutta, NCSU, Spring, 2007 7

Variable Number of Parameters Special type va_list to point to different arguments l Initialize

Variable Number of Parameters Special type va_list to point to different arguments l Initialize by va_start - after repeated access by va_arg, call va_end l /* return a product of double arguments */ double product(int number, . . . ) { va_list; double p; int i; va_start(list, number); for(i = 0, p = 1. 0; i < number; i++) p *= va_arg(list, double); va_end(list); return p; } Copyright Rudra Dutta, NCSU, Spring, 2007 8