ME964High Performance Computing for Engineering Applications“Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.” Dr. Seuss© Dan Negrut, 2012ME964 UW-MadisonQuick Overview of C ProgramminggdbMercurialLogging in to EulerThe Eclipse IDEJanuary 31, 2012Before We Get Started… Last time Brief overview of C Most important to understand: handling pointers in C Today Wrap up quick overview of C Programming Super quick intro to gdb (debugging tool under Linux) Learn how to login into Euler Quick intro on Mercurial for revision control for handling of your assignments Getting started with Eclipse, an integrated development environment First assignment sent out last week, available on the class website HW 1 due on Th, at 11:59 PM Post related questions to the forum2sizeof, malloc, memset, memmove/* allocating a struct with malloc() *//* allocating a struct with malloc() *//* allocating a struct with malloc() *//* allocating a struct with malloc() */struct my_struct *s = NULL;struct my_struct *s = NULL;struct my_struct *s = NULL;struct my_struct *s = NULL;s = (struct my_struct *)malloc(sizeof(*s)); /* NOT sizeof(s)!! */s = (struct my_struct *)malloc(sizeof(*s)); /* NOT sizeof(s)!! */s = (struct my_struct *)malloc(sizeof(*s)); /* NOT sizeof(s)!! */s = (struct my_struct *)malloc(sizeof(*s)); /* NOT sizeof(s)!! */if (s == NULL) {if (s == NULL) {if (s == NULL) {if (s == NULL) {printf(stderr, “no memory!”);printf(stderr, “no memory!”);printf(stderr, “no memory!”);printf(stderr, “no memory!”);exit(1);exit(1);exit(1);exit(1);}}}}memset(s, 0, sizeof(*s));memset(s, 0, sizeof(*s));memset(s, 0, sizeof(*s));memset(s, 0, sizeof(*s));/* another way to initialize an alloc’d structure: *//* another way to initialize an alloc’d structure: *//* another way to initialize an alloc’d structure: *//* another way to initialize an alloc’d structure: */struct my_struct init = {struct my_struct init = {struct my_struct init = {struct my_struct init = {counter: 1,counter: 1,counter: 1,counter: 1,average: 2.5,average: 2.5,average: 2.5,average: 2.5,in_use: 1in_use: 1in_use: 1in_use: 1};};};};/* memmove(dst, src, size) (note, arg order like assignment) *//* memmove(dst, src, size) (note, arg order like assignment) *//* memmove(dst, src, size) (note, arg order like assignment) *//* memmove(dst, src, size) (note, arg order like assignment) */memmove(s, &init, sizeof(init));memmove(s, &init, sizeof(init));memmove(s, &init, sizeof(init));memmove(s, &init, sizeof(init));/* when you are done with it, free it! *//* when you are done with it, free it! *//* when you are done with it, free it! *//* when you are done with it, free it! */free(s);free(s);free(s);free(s);s = NULL;s = NULL;s = NULL;s = NULL;sizeof() can take a variable reference in place of a type name. This guarantees the right allocation, but don’t accidentally allocate the sizeof() the pointer instead of the object!malloc() does not zero the memory, so you should memset() it to 0.Always check for NULL.. Even if you just exit(1).malloc() allocates n bytesWhy?memmove is preferred because it is safe for shifting buffersWhy?Use pointers as implied in-use flags!3Function TypesAnother less obvious construct is the “pointer to function” type.For example, qsort: (a sort function in the standard library)void qsort(void *base, size_t nmemb, size_t size,void qsort(void *base, size_t nmemb, size_t size,void qsort(void *base, size_t nmemb, size_t size,void qsort(void *base, size_t nmemb, size_t size,int (*compar)(const void *, const void *));int (*compar)(const void *, const void *));int (*compar)(const void *, const void *));int (*compar)(const void *, const void *));/* function matching this type: *//* function matching this type: *//* function matching this type: *//* function matching this type: */int cmp_function(const void *x, const void *y);int cmp_function(const void *x, const void *y);int cmp_function(const void *x, const void *y);int cmp_function(const void *x, const void *y);/* typedef defining this type: *//* typedef defining this type: *//* typedef defining this type: *//* typedef defining this type: */typedef int (*cmp_type) (const void *, const void *);typedef int (*cmp_type) (const void *, const void *);typedef int (*cmp_type) (const void *, const void *);typedef int (*cmp_type) (const void *, const void *);/* rewrite qsort prototype using our typedef *//* rewrite qsort prototype using our typedef *//* rewrite qsort prototype using our typedef *//* rewrite qsort prototype using our typedef */void qsort(void *base, size_t nmemb, size_t size, cmp_type compar);void qsort(void *base, size_t nmemb, size_t size, cmp_type compar);void qsort(void *base, size_t nmemb, size_t size, cmp_type compar);void qsort(void *base, size_t nmemb, size_t size, cmp_type compar);The last argument is a comparison functionconst means the function is not allowed to modify memory via this pointer.void * is a pointer to memory of unknown type. size_t is an unsigned int4+3 +2 +1 +0sparr[1]arr[0]= 932= 5= 266p[2] = 15= 15Example: Using Pointers Arithmetic9009049089129169209249289329369409445#include <iostream>int main(){int arr[2]={266,5};int * p;short s;p = (int*) malloc(sizeof(int)*3);p[2] = arr[1] * 3;s = (short)( *(p+2) );free( p );p=NULL;return 0;}+3 +2 +1 +0sparr[1]arr[0]= 932= 5= 266p[2] = 15= 15Q: what if you say p[2]=0 right after you free the memory?A: code runs, weird stuff about to happen though.Q: what if you do not call free(p)?A: memory leak.Q: why does “s” assume the value it does?9009049089129169209249289329369409446Example: Using Pointers Arithmetic[Cntd.]Q: what if you say p[2]=0 right after you set p to NULL?A: code crashes (no compile time warning…)sparr[1]arr[0]= 932= 5= 266p[2] = 37198203= -26245Q: what is the value of “s” now, and why?A:int dummy = 12399401p = (int*) malloc(sizeof(int)*3);p[2] = dummy * 3;short s = (short)( *(p+2) );free( p );p,3 932 int *[0] -842150451 int[1] -842150451 int[2] 37198203 ints -26245 short+3 +2 +1 +09009049089129169209249289329369409447Example: Using Pointers Arithmetic[End]High Level Question: Why is Software Hard? Complexity: Every conditional (“if”) doubles the number of paths through your code, every bit of state doubles possible states. Parallel programming makes it significantly harder to write good code… Recommendation: reuse code with functions, avoid duplicate state
View Full Document