Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage...
Transcript of Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage...
![Page 1: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/1.jpg)
Introduction to CMemory Model
Instructor: Yin Lou
02/04/2011
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 2: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/2.jpg)
Recap: Pointers
I int *ptr;
I Pointers are variables that store memory address of othervariables
I Type of variable pointed to depends on type of pointer:
I int *ptr points to an integer valueI char *ptr points to character variableI Can cast between pointer types: myIntPtr = (int *)
myOtherPtr;I void *ptr has an unspecified type (generic pointer); must be
cast to a type before used
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 3: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/3.jpg)
Recap: Pointers
I int *ptr;
I Pointers are variables that store memory address of othervariables
I Type of variable pointed to depends on type of pointer:I int *ptr points to an integer valueI char *ptr points to character variable
I Can cast between pointer types: myIntPtr = (int *)myOtherPtr;
I void *ptr has an unspecified type (generic pointer); must becast to a type before used
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 4: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/4.jpg)
Recap: Pointers
I int *ptr;
I Pointers are variables that store memory address of othervariables
I Type of variable pointed to depends on type of pointer:I int *ptr points to an integer valueI char *ptr points to character variableI Can cast between pointer types: myIntPtr = (int *)
myOtherPtr;
I void *ptr has an unspecified type (generic pointer); must becast to a type before used
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 5: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/5.jpg)
Recap: Pointers
I int *ptr;
I Pointers are variables that store memory address of othervariables
I Type of variable pointed to depends on type of pointer:I int *ptr points to an integer valueI char *ptr points to character variableI Can cast between pointer types: myIntPtr = (int *)
myOtherPtr;I void *ptr has an unspecified type (generic pointer); must be
cast to a type before used
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 6: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/6.jpg)
Recap: Pointers
I Two main operationsI * dereference: get the value at the memory location stored in
a pointerI & address of: get the address of a variableI int *myPtr = &myVar;
I Pointer arithmetic: directly manipulate a pointer’s content toaccess other locations
I Use with caution!: can access bad areas of memory andcause a crash
I However, it is useful in accessing and manipulating datastructures
I Can have pointers to pointersI int **my2dArray;
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 7: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/7.jpg)
Memory
I Program codeI Function variables
I ArgumentsI Local variablesI Return location
I Global VariablesI Statically allocatedI Dynamically allocated
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 8: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/8.jpg)
The Stack
Stores
I Function local variables
I Temporary variables
I Arguments for next function call
I Where to return when function ends
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 9: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/9.jpg)
The Stack
Managed by compiler
I One stack frame each time function called
I Created when function called
I Stacked on top (under) one another
I Destroyed at function exit
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 10: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/10.jpg)
What Can Go Wrong?
char *my_strcat(char *s1, char *s2)
{
char s3[1024];
strcpy(s3, s1);
strcat(s3, s2);
return s3;
}
I Recall that local variables are stored on the stack
I Memory for local variables is deallocated when functionreturns
I Returning a pointer to a local variable is almost always a bug!
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 11: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/11.jpg)
What Can Go Wrong?
char *my_strcat(char *s1, char *s2)
{
char s3[1024];
strcpy(s3, s1);
strcat(s3, s2);
return s3;
}
I Recall that local variables are stored on the stack
I Memory for local variables is deallocated when functionreturns
I Returning a pointer to a local variable is almost always a bug!
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 12: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/12.jpg)
What Can Go Wrong?
I Run out of stack spaceI Unintentionally change values on the stack
I In some other function’s frameI Even return address from function
I Access memory even after frame is deallocated
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 13: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/13.jpg)
The Heap
I C can use space in another part of memory: the heap
I The heap is separate from the execution stackI Heap regions are not deallocated when a function returnsI Note: this is completely unrelated to the Heap data structure
I The programmer requests storage space on the heapI C never puts variables on the heap automaticallyI But local variables might point to locations on the heapI Heap space must be explicitly allocated and deallocated by the
programmer
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 14: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/14.jpg)
The Heap
I C can use space in another part of memory: the heapI The heap is separate from the execution stackI Heap regions are not deallocated when a function returnsI Note: this is completely unrelated to the Heap data structure
I The programmer requests storage space on the heapI C never puts variables on the heap automaticallyI But local variables might point to locations on the heapI Heap space must be explicitly allocated and deallocated by the
programmer
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 15: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/15.jpg)
The Heap
I C can use space in another part of memory: the heapI The heap is separate from the execution stackI Heap regions are not deallocated when a function returnsI Note: this is completely unrelated to the Heap data structure
I The programmer requests storage space on the heapI C never puts variables on the heap automaticallyI But local variables might point to locations on the heapI Heap space must be explicitly allocated and deallocated by the
programmer
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 16: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/16.jpg)
malloc
I Library function in stdlib.hI Stands for memory allocate
I Requests a memory region of a specified sizeI Syntax: void *malloc(int size)I void * is generic pointer type
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 17: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/17.jpg)
malloc
I Library function in stdlib.hI Stands for memory allocate
I Requests a memory region of a specified sizeI Syntax: void *malloc(int size)I void * is generic pointer type
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 18: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/18.jpg)
Usage
int main()
{
int *p = (int *) malloc(10 * sizeof(int));
if (p == NULL)
{
// do cleanup
}
// do something
free(p);
return 0;
}
I MUST check the return value from malloc
I MUST explicitly free memory when no longer in use
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 19: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/19.jpg)
Usage
int main()
{
int *p = (int *) malloc(10 * sizeof(int));
if (p == NULL)
{
// do cleanup
}
// do something
free(p);
return 0;
}
I MUST check the return value from malloc
I MUST explicitly free memory when no longer in use
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 20: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/20.jpg)
What Can Go Wrong?
I Run out of heap space: malloc returns 0
I Unintentionally change other heap data
I Access memory after free’d
I free memory twice
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 21: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/21.jpg)
Usage
int main()
{
int *p = (int *) malloc(10 * sizeof(int));
if (p == NULL)
{
// do cleanup
}
// do something
if (p != NULL)
{
free(p);
p = NULL;
}
return 0;
}
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 22: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/22.jpg)
Garbage Collection in C
I Pointers make garbage collection difficult or impossibleI Its very difficult to determine whether memory is still being
usedI Javas references are a restricted form of pointers that don’t
allow arithmetic, just because of this issueI There are garbage collecting libraries for C, but they aren’t
guaranteed to work with any program
Example
char *s = (char *) malloc(1024);
s -= 10000;
// nothing points to the allocated memory
// region. Could it be garbage collected?
s += 10000;
// no, because now something points to it again!
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 23: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/23.jpg)
Garbage Collection in C
I Pointers make garbage collection difficult or impossibleI Its very difficult to determine whether memory is still being
usedI Javas references are a restricted form of pointers that don’t
allow arithmetic, just because of this issueI There are garbage collecting libraries for C, but they aren’t
guaranteed to work with any program
Example
char *s = (char *) malloc(1024);
s -= 10000;
// nothing points to the allocated memory
// region. Could it be garbage collected?
s += 10000;
// no, because now something points to it again!
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 24: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/24.jpg)
Multidimensional Arrays
I On the stack: int a[10][20];
I Initialization: int a[][] = {{1, 2, 3}, {4, 5, 6}};
I Accessing the array: a[1][0]
I On the heap
int **a = (int **) malloc(10 * sizeof(int *));
for (int i = 0; i < 10; ++i)
{
a[i] = (int *) malloc(20 * sizeof(int));
}
I Don’t forget to free them!
Introduction to C CS 2022, Spring 2011, Lecture 6
![Page 25: Introduction to C - cs.cornell.edu · Introduction to C CS 2022, Spring 2011, Lecture 6. Garbage Collection in C I Pointers make garbage collection di cult or impossible I Its very](https://reader031.fdocuments.us/reader031/viewer/2022041402/5e17a6f374ab5433053fd7a5/html5/thumbnails/25.jpg)
Multidimensional Arrays
I On the stack: int a[10][20];
I Initialization: int a[][] = {{1, 2, 3}, {4, 5, 6}};
I Accessing the array: a[1][0]
I On the heap
int **a = (int **) malloc(10 * sizeof(int *));
for (int i = 0; i < 10; ++i)
{
a[i] = (int *) malloc(20 * sizeof(int));
}
I Don’t forget to free them!
Introduction to C CS 2022, Spring 2011, Lecture 6