Unformatted text preview:

1CMSC 212 – S05 (lect 15)Announcementsz Program #3– Is on the webz Reading– Chapter 16, 13.3 (Today)• Skip 16.4-16.616,9– Chapter 17 (Thursday)2CMSC 212 – S05 (lect 15)Additional Standard Library Functionsz Random Numbers– void srand(unsigned int);• seed the random number generator– int rand(void);• return a pseudo-random number between 0 and RAND_MAXz Floating Point (all use doubles)– prototypes defined in math.h– double sqrt(double value);• compute square root of value• Bad parameters produce domain errors• stderr is set to EDOM– double exp(double value) and double exp10(double value)• computes evalue and 2value3CMSC 212 – S05 (lect 15)Trig Functionsz double sin(double angle);– computes sin of angle (in radians)z Also have:– double cos(double angle);– double tan(double angle);– double asin(double value);– double acos(double value);– double atan(double value);4CMSC 212 – S05 (lect 15)Other Handy Functionsz Fractions– double floor(double x);• next lowest whole integer– double ceil(double x);• next highest whole integer– double fabs(double x);• absolute value– double fmod(double x, double y);• restricts y to an integer5CMSC 212 – S05 (lect 15)Function Pointersz Pointers can also be of type function– void (*myVoidFunc)(int);• declare a variable myVoidFunc which points to a function which takes an integer as a parameter.• Like any pointer variable, declaring it does not create an instance of what it points to!z Uses– Create Object Oriented Code– Callbacks from utility routinesz Often a good idea to typedef each function pointer– typedef void(*myVoidFuncPtr)(int);6CMSC 212 – S05 (lect 15)Function Pointer Exampleunion myDataType {int a;float b;} ;typedef void (*myPrintFuncPtr)(union myDataType);typedef struct {myPrintFuncPtr printIt;union myDataType data;} myObject ;void printInt(union myDataType data) {printf("data = %d\n", data.a);}7CMSC 212 – S05 (lect 15)Function Pointer Example Continuedint main(void) {int i;myObject *objects;objects = (myObject *) calloc(sizeof(myObject), 5);objects[0].data.a = 43;objects[0].printIt = printInt;objects[1].data.b = 3.1415;objects[1].printIt = printFloat;for (i=0; i < 2; i++) {objects[i].printIt(objects[i].data);}exit(0);}8CMSC 212 – S05 (lect 15)Callback Exampletypedef int (*compareFunc)(item *a, item * b);int searchTree(Node *root, Item *target, compareFunc cmp) {if (!root) return -1;ret = (cmp)(a, root->data);if (ret == 0) {/* found it */return 1;} else if (ret < 0) {return searchTree(root->left, target, cmp);} else {return searchTree(root->right, target, cmp);}}9CMSC 212 – S05 (lect 15)Date & Time Functionsz clock_t clock(void);– process time since start of program execution– to convert to time, use CLOCKS_PER_SECz time_t time(time_t *val);– fill val with the current time (in machine dependent format)z char *ctime(time_t *val);– return a character representation of the passed time– Sun Jul 4 04:02:48 2005\n\0z double difftime(time_t time1, time_t time2)– return number of seconds between time1 and time2z struct tm *gmtime(time_t val)struct tm*localtime(time_t val)– convert to UTC or local time10CMSC 212 – S05 (lect 15)Execution Environmentz Program Termination– abort(void);• terminate program with error (usually a core time)– atexit(void (func)(void));• on termination call function func–exit(int status);z Running Shell Commands– void system(char *command);• runs command (not all systems support it)z Sorting– void qsort(void *base, size_t number, size_t elementSize, int (*compare)(void const *, void const *));• Sort the passed array, using passed compare function• strcmp will work for this!11CMSC 212 – S05 (lect 15)Creating your own librariesz Libraries– collections of object (.o) files that work together– can be linked into other programs• linking can happen prior to execution• linking can be done during program executionz Static libraries– linked into the program as part of the link step– require space in each executable that uses themz Dynamic (shared) libraries– linked into the program at program startup– require only one copy for the entire system– Have version numbers associated with them• controls which version work with which applications12CMSC 212 – S05 (lect 15)Creating Static Librariesz UNIX Command ar– LIBRARY=myLib.a– OBJS=file1.o file2.o file3.o– ar cru $(LIBRARY) $(OBJS)z Using a Static Library– gcc -o myProgram main.o myLib.a13CMSC 212 – S05 (lect 15)Creating Dynamic (shared) Librariesz Use special gcc flags– -nostdlib -shared -fPIC -Wl,-soname,mylib.so.1• -nostdlib - no standard C library needed• -shared - generate a shared library• -fPIC - generate position independent code• -Wl,-soname,mylib.so.1– name the shared object mylib.so.1z Example Makefile RulesLIBFLAGS = -nostdlib -shared -fPIC -Wl,-soname,[email protected]: avl.c table.h$(CC) $(LIBFLAGS) $(CFLAGS) avl.c -o libavl.soln -f -s libavl.so libavl.so.114CMSC 212 – S05 (lect 15)Runtime Linkingz Can Load a library at runtime– Allows skins, plugins, etc to workz C functions to support this:– void * dlopen(const char *pathname, int mode)• pathname is the name of a shared library• mode controls operation (RTLD_NOW)– void *dlsym(void *handle, const char *name);• lookup a function by name in the passed shared library• Return a pointer to that function (or NULL if not found)– int dlcose(void *handle);• returns 0 on sucess15CMSC 212 – S05 (lect 15)Dlopen Exampletypedef void (*displayFuncPtr)(char *file);typedef struct {displayFuncPtr displayFunc; char *mediaType;} renderTypePlugin;renderTypePlugin *loadPlugin(char *library, char *mediaType) {void *dlPtr; renderTypePlugin *plugin;dlPtr = dlopen(library, RTLD_NOW);if (!ptr) return NULL;/* … allocate plugin and check return …*/plugin->displayFunc = (displayFuncPtr ) dlsym(dlPtr, "draw");if (!plugin->displayFunc) /* error, couldn't find function */plugin->mediaType = strdup(mediaType);dlcose(dlPtr);return


View Full Document

UMD CMSC 212 - Lecture Slides

Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?