Download presentation
Presentation is loading. Please wait.
1
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB
2
2 Example 11 #include 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-allocation Explicit allocation and de-allocation
3
3 int *ptr; ? ptr 4000 ptr = (int*)malloc(4 * sizeof(int)); ??? ? 6012600860046000 *ptr=4; 4 free(ptr); Explicit memory allocation
4
4 Dynamic array int *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; }
5
5 Array of Pointers Variable length strings Variable length strings /* card[4] => array of 4 elements char* => element is a pointer to a character. char* => element is a pointer to a character. *card[4] => array of 4 pointers */ *card[4] => array of 4 pointers */ char *card[4]; card[3]4012 card[2]4008 card[1]4004 card[0]4000 NULL
6
6 card[0] = (char*)malloc(6*sizeof(char)); card[1] = (char*)malloc(3*sizeof(char));... Static allocation of a 2D array: Static allocation of a 2D array: char card[4][10]; //waste of space Array of Pointers
7
7 Common errors - Memory leak int *ptr, x; //ptr points to newly allocated memory ptr = (int*)malloc(10*sizeof(int)); // ptr now points away from allocated memory // ptr now points away from allocated memory ptr = &x; Memory allocated with malloc is no longer available for use by the program. Memory allocated with malloc is no longer available for use by the program. Released only when program quits. 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. Becomes a problem in large programs where a large number of variables are created and destroyed during the execution of the program.
8
8 Common errors - Dangling pointers int *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 access either of them can cause logical errors */ either of them can cause logical errors */free(x); x = NULL; // One way to prevent incorrect access i = NULL;
9
9 Functions – pointers as arguments Example 12 #include 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 */ }
10
10 a 4 4000 b 5 4004 c 6 4008 ptr 4004 4012 In main() pa 4000 6000 pb 4004 6004 pc 4008 6008 In function
11
11 a 4 4000 b 5 4004 c 7 4008 ptr 4004 4012 In main() after the function call
12
12 What’s wrong with this ? Example 13 #include 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 */
13
13 p ? 4000 ptr 6000 ? temp 6004 8 In main() In the function
14
14 p ? 4000 In main() after the function call
15
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= 8; int temp= 8; *ptr = (int*)malloc(sizeof(int)); *ptr = (int*)malloc(sizeof(int)); **ptr = temp; **ptr = temp;}
16
16 p ? 4000 ptr 6000 temp 6004 8 4000 In main() In the function ? 8000 On the heap 8000 8
17
17 p ? 4000 In main() In the function ? 8000 On the heap 8000 8
18
18 Functions - Passing and returning arrays Example 14 #include 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 address for (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; }
19
19 Passing/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 ? */
20
20 GNU debugger (gdb) Tutorial and reference sheet on class website Tutorial and reference sheet on class website –…/Homework/Sec-01/gdbtutorial.htm –…/Homework/Sec-01/gdb-reference-card.pdf
21
Homework 4 and 5 More practice on C. More practice on C. Available on class’s Angel page Available on class’s Angel page – follow link from the schedule page 21
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.