Download presentation
Presentation is loading. Please wait.
1
Memory allocation & parameter passing
CSSE 332 Operating Systems Rose-Hulman Institute of Technology Pass out quiz on Memory Management 1
2
Announcements HW 2 is due Thursday at 11:59 PM
Run svn st at a terminal to check the status of your repo. 2
3
Explicit memory allocation
>> cd Examples/ >> emacs example11.c Study the use of malloc() and free() They allow explicit allocation and de-allocation of dynamic memory >> gcc example11.c –o example11 >> ./example11 malloc is defined in section 3 of the manual pages. >> man –s 3 malloc Man formats and displays the manual pages for some name that you pass to it. >> man man Really, a pointer to an array of integers. 3
4
Explicit memory allocation (2)
int *ptr; ptr = (int*)malloc(4 * sizeof(int)); *ptr=4; 6000 6004 6008 6012 ptr 6000 ? ? ? ? ? 4 Code from example 11 Might want to display *ptr. 4000 free(ptr); 4
5
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; } Might be able to modify example 11 to include this and experiment with this code. Might want to display the content of the array. 5
6
Array of Pointers Variable length strings char *card[4];
/* card[4] => array of 4 elements char* => element is a pointer to a character. *card[4] => array of 4 pointers */ char *card[4]; 4000 card[0] NULL 4004 card[1] NULL 4008 card[2] NULL 4012 card[3] NULL Q1 6
7
Array of Pointers (2) card[0] = (char *)malloc(6 * sizeof(char));
. . . Static allocation of a 2D array: char card[4][10]; // waste of space 7
8
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 = &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. Use box and pointer diagrams to illustrate 8
9
Common errors - Dangling pointers
int *i, *x; i = (int*)malloc( 5 * sizeof(int)); x = i; //both point to the same address. /* i is a dangling pointer; trying to access either i or x can cause logical errors */ free(x); x = NULL; // One way to prevent incorrect access i = NULL; 9
10
Functions – pointers as arguments
>> cd Examples/ >> emacs example12.c Study the code to see how pointers are passed to functions. >> gcc example12.c –o example12 >> ./example12 The only value that’s changed is the value of c because the pointee pointed to by pc is c. This is like making a function return multiple values. 10
11
In main() a 4 4000 b 5 4004 c 6 4008 ptr 4004 4012 a, b, c, and ptr are local variable in main() pa, pb, and pc are local variables in sumAndInc() pa 4000 6000 pb 4004 6004 pc 4008 6008 In function 11
12
In main() after the function call
4 4000 b 5 4004 c 7 4008 ptr 4004 4012 In main() after the function call 12
13
What’s wrong with this ? >> cd Examples/
>> emacs example13.c Does the code compile? Does it run correctly? >> gcc example13.c –o example13 >> ./example13 The code compiles correctly but when run, it gives a segmentation fault. Why? p is still uninitialized. Q2 13
14
p ? 4000 In main() ptr temp In the function 6004 6000 ? 6004 8
In function, temp is stored on the stack. 14
15
In main() after function call
p ? 4000 In main() after function call The stack frame has been popped so temp no longer exists. p is still uninitialized. This is why we receive a segmentation fault. 15
16
Passing & returning array
>> cd Examples/ >> emacs example14.c Why is the size of the array passed? Arrays are always passed as pointers? >> gcc example14.c –o example14 >> ./example14 An array does not know its size. Recall that an array is a pointer to a set of contiguous memory locations. 16
17
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 ? */ Q3 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.