Download CSE 333 - Lecture 5 - malloc, free, struct, typedef and more Exams Computer science in PDF only on Docsity!
CSE333 lec 5 C.4 // 06-27-12 // perkins
CSE 333
Lecture 5 - malloc, free, struct, typedef
Hal Perkins
Department of Computer Science & Engineering
University of Washington
CSE333 lec 5 C.4 // 06-27-12 // perkins
Administrivia
HW1 is due in 8 days: Thursday, July 5, 11 pm
- (^) need to make steady progress
- see course overview web page for the late policy - but try not
to use late days this early in the quarter
Be sure to check out the course discussion board
- (^) Suggestion: “mail me whenever a new posting appears”
setting if you don’t want to browse regularly
- (^) Please post questions here, rather than emailing me or the
TAs, so that everybody benefits from answers
CSE333 lec 5 C.4 // 06-27-12 // perkins
Today’s goals:
- (^) understand heap-allocated memory
‣ malloc( ), free( )
‣ memory leaks
- (^) quick intro to structs and typedef
CSE333 lec 5 C.4 // 06-27-12 // perkins
Memory allocation
So far, we have seen two kinds of memory allocation:
// a global variable int counter = 0 ; int main(int argc, char **argv) { counter++; return 0 ; } int foo(int a) { int x = a + 1 ; // local var return x; } int main(int argc, char **argv) { int y = foo( 10 ); // local var return 0 ; } counter is statically allocated
- allocated when program is loaded
- (^) deallocated when program exits a,x,y are automatically allocated
- allocated when function is called
- (^) deallocated when function returns
CSE333 lec 5 C.4 // 06-27-12 // perkins
Dynamic allocation
What we want is dynamically allocated memory
- (^) your program explicitly requests a new block of memory
‣ the language runtime allocates it, perhaps with help from OS
- (^) dynamically allocated memory persists until:
‣ your code explicitly deallocates it [manual memory management]
‣ a garbage collector collects it [automatic memory management]
- (^) C requires you to manually manage memory
‣ gives you more control, but causes headaches
CSE333 lec 5 C.4 // 06-27-12 // perkins
C and malloc
variable = (type *) malloc(size in bytes);
malloc allocates a block of memory of the given size
- (^) returns a pointer to the first byte of that memory
‣ malloc returns NULL if the memory could not be allocated
- (^) you should assume the memory initially contains garbage
- (^) normally use sizeof to calculate the size you need // allocate a 10-float array float *arr = (float *) malloc( 10 *sizeof(float)); if (arr == NULL) return errcode; arr[ 0 ] = 5.1; // etc.
CSE333 lec 5 C.4 // 06-27-12 // perkins
Deallocation
free(pointer);
Releases the memory pointed-to by the pointer
- (^) pointer must point to the first byte of heap-allocated memory
‣ i.e., something previously returned by malloc( ) or calloc( )
- (^) after free( )‘ing a block of memory, that block of memory might be
returned in some future malloc( ) / calloc( )
- (^) common habit: set a pointer to NULL after freeing it
‣ (guards against some dangling pointer bugs, but code should be ok without it)
long *arr = (long *) calloc( 10 , sizeof(long)); if (arr == NULL) return errcode; // .. do something .. free(arr); arr = NULL;
CSE333 lec 5 C.4 // 06-27-12 // perkins
Heap
The heap (aka “free store”)
- (^) is a large pool of unused
memory that is used for
dynamically allocated data
- (^) malloc allocates chunks of
data in the heap, free
deallocates data
- (^) malloc maintains book-
keeping data in the heap to
track allocated blocks
0x 0xFFFFFFFF
OS kernel [protected]
stack
shared libraries
heap (malloc/free)
read/write segment
.data, .bss
read-only segment
.text, .rodata
CSE333 lec 5 C.4 // 06-27-12 // perkins
Heap + stack OS kernel [protected]
stack
heap (malloc/free)
read/write segment
globals
read-only segment
(main, f, g)
arraycopy.c
main
argc, argv nums ncopy 2 4 6 8 #include <stdlib.h> int *copy(int a[], int size) { int i, *a2; a2 = (int *)malloc( size * sizeof(int)); if (a2 == NULL) return NULL; for (i = 0 ; i < size; i++) a2[i] = a[i]; return a2; } int main(...) { int nums[ 4 ] = { 2 , 4 , 6 , 8 }; int *ncopy = copy(nums, 4 ); // ... do stuff ... free(ncopy); return 0 ; }
CSE333 lec 5 C.4 // 06-27-12 // perkins
Heap + stack OS kernel [protected]
stack
heap (malloc/free)
read/write segment
globals
read-only segment
(main, f, g)
arraycopy.c
main
argc, argv nums ncopy 2 4 6 8
copy
i a #include <stdlib.h> int *copy(int a[], int size) { int i, *a2; a2 = (int *)malloc( size * sizeof(int)); if (a2 == NULL) return NULL; for (i = 0 ; i < size; i++) a2[i] = a[i]; return a2; } int main(...) { int nums[ 4 ] = { 2 , 4 , 6 , 8 }; int *ncopy = copy(nums, 4 ); // ... do stuff ... free(ncopy); return 0 ; }
CSE333 lec 5 C.4 // 06-27-12 // perkins
Heap + stack OS kernel [protected]
stack
heap (malloc/free)
read/write segment
globals
read-only segment
(main, f, g)
arraycopy.c
main
argc, argv nums ncopy 2 4 6 8
copy
i a #include <stdlib.h> int *copy(int a[], int size) { int i, *a2; a2 = (int *)malloc( size * sizeof(int)); if (a2 == NULL) return NULL; for (i = 0 ; i < size; i++) a2[i] = a[i]; return a2; } int main(...) { int nums[ 4 ] = { 2 , 4 , 6 , 8 }; int *ncopy = copy(nums, 4 ); // ... do stuff ... free(ncopy); return 0 ; }
CSE333 lec 5 C.4 // 06-27-12 // perkins
Heap + stack OS kernel [protected]
stack
heap (malloc/free)
read/write segment
globals
read-only segment
(main, f, g)
arraycopy.c
main
argc, argv nums ncopy 2 4 6 8
copy
i a #include <stdlib.h> int *copy(int a[], int size) { int i, *a2; a2 = (int *)malloc( size * sizeof(int)); if (a2 == NULL) return NULL; for (i = 0 ; i < size; i++) a2[i] = a[i]; return a2; } int main(...) { int nums[ 4 ] = { 2 , 4 , 6 , 8 }; int *ncopy = copy(nums, 4 ); // ... do stuff ... free(ncopy); return 0 ; }
CSE333 lec 5 C.4 // 06-27-12 // perkins
Heap + stack OS kernel [protected]
stack
heap (malloc/free)
read/write segment
globals
read-only segment
(main, f, g)
arraycopy.c
main
argc, argv nums ncopy 2 4 6 8
copy
i a 2 4 6 8 #include <stdlib.h> int *copy(int a[], int size) { int i, *a2; a2 = (int *)malloc( size * sizeof(int)); if (a2 == NULL) return NULL; for (i = 0 ; i < size; i++) a2[i] = a[i]; return a2; } int main(...) { int nums[ 4 ] = { 2 , 4 , 6 , 8 }; int *ncopy = copy(nums, 4 ); // ... do stuff ... free(ncopy); return 0 ; }
CSE333 lec 5 C.4 // 06-27-12 // perkins
Heap + stack OS kernel [protected]
stack
heap (malloc/free)
read/write segment
globals
read-only segment
(main, f, g)
arraycopy.c
main
argc, argv nums ncopy 2 4 6 8
copy
i a 2 4 6 8 #include <stdlib.h> int *copy(int a[], int size) { int i, *a2; a2 = (int *)malloc( size * sizeof(int)); if (a2 == NULL) return NULL; for (i = 0 ; i < size; i++) a2[i] = a[i]; return a2; } int main(...) { int nums[ 4 ] = { 2 , 4 , 6 , 8 }; int *ncopy = copy(nums, 4 ); // ... do stuff ... free(ncopy); return 0 ; }