CSSE 332Explicit Memory Allocation, Parameter passing, and GDB2Example 11#include <stdio.h>int main(int argc, char *argv[]){int *ptr;/* allocate space to hold an int */ptr = (int*)malloc(4 * sizeof(int)); /* do stuff with the space */*ptr=4; //ptr[0] = 4;/* free up the allocated space */free(ptr);return 0;}Explicit memory allocation Explicit allocation and de-allocation3int *ptr;?ptr4000ptr = (int*)malloc(4 * sizeof(int));????60126008600460006000*ptr=4;4free(ptr);Explicit memory allocation4Dynamic arrayint *ptr, i,size;printf(“Enter the size of the array”);scanf(“%d”, &size)ptr = (int *) malloc( size * sizeof(int) );for(i=0; i<size; i++){ptr[i] = i;}5Array of Pointers Variable length strings/* card[4] => array of 4 elementschar* => element is a pointer to a character.*card[4] => array of 4 pointers */char *card[4]; card[3]4012card[2]4008card[1]4004card[0]4000NULLNULLNULLNULL6card[0] = (char*)malloc(6*sizeof(char));card[1] = (char*)malloc(3*sizeof(char)); . . . Static allocation of a 2D array:char card[4][10]; //waste of spaceArray of Pointers7Common errors - Memory leakint *ptr, x; //ptr points to newly allocated memoryptr = (int*)malloc(10*sizeof(int)); // ptr now points away from allocated memory ptr = &x; Memory allocated with malloc is no longer available for use by the program. Released only when program quits. Becomes a problem in large programs where a large number of variables are created and destroyed during the execution of the program.8Common errors - Dangling pointersint *i, *x;i = (int*)malloc( 5 x sizeof(int));x = i; //both point to the same address./* both i and x are dangling pointers; trying to accesseither of them can cause logical errors */free(x);x = NULL; // One way to prevent incorrect access i = NULL;9Functions – pointers as argumentsExample 12#include <stdio.h>int sumAndInc(int *pa, int *pb,int* pc); int main(int argc, char *argv[]){int a=4, b=5, c=6;int *ptr = &b;/* call the function */int total = sumAndInc(&a,ptr,&c); printf(“The sum of %d and %d is %d and c is %p\n”,a, b, total, c);}/* pointers as arguments */int sumAndInc(int *pa, int *pb,int *pc ){*pc = *pc+1; // return a pointee value; NOT *(pc+1)return (*pa+*pb); /* return by value */}10a44000b54004c64008ptr40044012In main()pa40006000pb40046004pc40086008In function11a44000b54004c74008ptr40044012In main() after the function call12What’s wrong with this ?Example 13#include <stdio.h>void DoSomething(int *ptr);int main(int argc, char *argv[]) {int *p;DoSomething(p);printf(“%d”, *p); /* will this work ? */return 0;}/* passed and returned using pointers */void DoSomething(int *ptr){ int temp= 5+3;ptr = &temp;}/* compiles correctly, but gives incorrect output */13p?4000ptr6000?temp600486004In main()In the function14p?4000In main() after the function callint main(int argc, char *argv[]) {int *p;DoSomething( &p );printf(“%d”, *p); /* will this work ? */return 0;}/* passed and returned using pointers */void DoSomething(int **ptr){ int temp= 8;*ptr = (int*)malloc(sizeof(int));**ptr = temp;}16p?4000ptr6000temp600484000In main()In the function?8000On the heap8000817p?4000In main()In the function?8000On the heap8000818Functions - Passing and returning arraysExample 14#include <stdio.h>void init_array( int array[], int size ) ;int main(int argc, char *argv[] ){int list[5];init_array(list, 5); //No & because list //is already an addressfor (i = 0; i < 5; i++) printf(“next:%d”, list[i]);}/* why size ? */void init_array(int array[], int size) { /* arrays ALWAYS passed as pointers */int i;for (i = 0; i < size; i++) array[i] = i; }19Passing/Returning a structure/* pass struct by value */void displayYear_1(struct birthday mybday) {printf(“I was born in %d\n”, mybday.year);}/* - inefficient: why ? *//* pass pointer to struct */void displayYear_2(struct birthday *pmybday) {printf(“I was born in %d\n”, pmybday->year);/* Note: „->‟, not „.‟, after a struct pointer */ }/* return struct by value */struct birthday get_bday(void){struct birthday newbday;newbday.year=1971; /* „.‟ after a struct */return newbday;}/* - also inefficient: why ? */20GNU debugger (gdb) Tutorial and reference sheet on class website– …/Homework/Sec-01/gdbtutorial.htm– …/Homework/Sec-01/gdb-reference-card.pdfHomework 4 and 5 More practice on C. Available on class’s Angel page– follow link from the schedule
View Full Document