Академический Документы
Профессиональный Документы
Культура Документы
MIT CSAIL
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Lecture plan
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Today
• What memory abstractions does C provide?
• What exactly is the distinction between stack and heap?
• How do I use pointers to access memory locations?
• How do I allocate and free memory on the heap?
• How should I use GDB and Valgrind?
Tomorrow
• How does the compiler actually work?
• What was that thing you did with == and =?
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Pointer syntax
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Practicing pronunciation
i n t ∗ xp , yp ;
int z ;
xp = &z ;
yp = xp ;
Set yp equal to xp. Now yp and xp “point to” the same value.
∗ xp = ∗ yp ;
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
void p r o c e s s v a r ( i n t x ) {
x = 5;
}
void fun () {
int x = 3;
process var (x) ;
p r i n t f ( ”%d\n” , x ) ;
}
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
/∗ P a s s i n g a p o i n t e r a s an argument . ∗/
v o i d p r o c e s s v a r ( i n t ∗ xp ) {
∗ xp = 5 ; /∗ The v a l u e o f xp . . . ∗/
}
void fun () {
int x = 3;
p r o c e s s v a r (&x ) ; /∗ The a d d r e s s o f x . . . ∗/
p r i n t f ( ”%d\n” , x ) ;
}
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Stack Heap
Memory is allocated Upon entering function With malloc
Memory is deallocated Upon function return With free
Addresses are assigned Statically Dynamically
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Managing memory
Conceptually
Keep track of what memory belongs to your program, making sure
• all addresses you give to other functions are valid for those
functions and
• you deallocate memory you are not using while you still know
about it.
While programming
• Use malloc to allocate memory on the heap if you will need it
after the current function returns.
• Use free to free pointers before you reassign them and lose
the pointer.
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Allocation
malloc is a C standard library function that finds a chunk of free
memory of the desired size and returns a pointer to it.
Deallocation
free marks the memory associated with a specific address as no
longer in use. (It keeps track of how much memory was associated
with that address!)
free (p) ;
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
int arr [ 5 ] ;
arr [1] = 6;
int ∗ arr ;
a r r = malloc ( s i z e o f ( i n t ) ∗ 5) ;
arr [1] = 5;
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
struct pair {
int f i r s t ;
i n t second ;
};
s t r u c t p a i r p1 ;
p1 . f i r s t = 0 ; /∗ Note t h e ” . ” s y n t a x . ∗/
s t r u c t p a i r ∗ pp = m a l l o c ( s i z e o f ( s t r u c t p a i r ) ) ;
( ∗ pp ) . f i r s t = 2 ;
pp−>s e c o n d = 3 ; /∗ Note t h e −> s y n t a c t i c s u g a r . ∗/
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
typedef struct p a i r p a i r t ;
pair t p;
typedef struct p a i r {
int f i r s t ;
i n t second ;
} pair t ;
enum
enum ANSWER { y e s , no , maybe } ;
enum ANSWER x = y e s ;
t y p e d e f enum BOOL { t r u e = 0 , f a l s e = 1 } b o o l t ;
bool t b = true ;
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
• Made up of nodes.
• Each node points to the next node.
• We have a pointer to the head.
• The last node points to nothing.
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Node structure
s t r u c t node {
int val ;
s t r u c t node ∗ n e x t ;
};
t y p e d e f s t r u c t node n o d e t ;
t y p e d e f s t r u c t node {
int val ;
s t r u c t node ∗ n e x t ;
} node t ;
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
/∗ R e t u r n s a new node w i t h t h e g i v e n v a l u e . ∗/
n o d e t ∗ make node ( i n t v a l ) {
/∗ U s i n g m a l l o c . ∗/
n o d e t ∗ new node = m a l l o c ( s i z e o f ( n o d e t ) ) ;
new node−>v a l = v a l ;
new node−>n e x t = NULL ;
r e t u r n new node ;
}
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Deleting a value
node t ∗ d e l e t e v a l ( int val , node t ∗ cur head , int ∗
succeeded ) {
∗ succeeded = 0;
i f ( c u r h e a d == NULL) {
r e t u r n NULL ;
} e l s e i f ( c u r h e a d −>v a l == v a l ) {
f r e e ( cur head ) ;
∗ succeeded = 1;
r e t u r n new head ;
} e l s e {
/∗ See c o d e on w e b s i t e . ∗/
}
}
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Memory errors
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
behavior.
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
==25520==
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
2 8
1 5 7
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
Recap Pointers Memory management Data structures Linked list example Tools and tips Goodbye
Until tomorrow. . .
Questions?
• The course staff will be available after class.
Eunsuk Kang and Jean Yang The Adventures of Malloc and New
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.