Beruflich Dokumente
Kultur Dokumente
Today
Computer Memory
Pointers/Addresses
Arrays
Memory Allocation
Today
Computer Memory
Pointers/Addresses
Arrays
Memory Allocation
3
Heap
Heap is a chunk of memory that users can use to
dynamically allocated memory.
Lasts until freed, or program exits.
Stack
return.
Memory Layout
Call Stack
//local variables
...
Call Stack
Lower address
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start,
int end){
//local variables
...
}
Top of Stack
8
Higher address
Call Stack
Lower address
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
int end){
Top of Stack
//local variables
...
Higher address
Call Stack
Lower address
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
}
void DrawLine(int start,
int end){
Top of Stack
main() book-keeping
//local variables
int i (DrawSquare arg)
...
}
10
Higher address
Call Stack
Lower address
Example:
void DrawSquare(int i){
int start, end, ...
DrawLine(start, end);
Top of Stack
DrawSquare
stack frame
...
}
main() book-keeping
11
Higher address
Call Stack
Lower address
Example:
void DrawSquare(int i){
DrawLine(start, end);
}
DrawSquare
stack frame
main() book-keeping
int i (DrawSquare arg)
...
}
Top of Stack
12
Higher address
Call Stack
Lower address
Example:
Top of Stack
DrawSquare book-keeping
DrawLine(start, end);
}
DrawSquare
stack frame
main() book-keeping
int i (DrawSquare arg)
...
}
13
Higher address
Call Stack
Lower address
Top of Stack
Example:
DrawLine
DrawSquare book-keeping
stack
frame
DrawLine(start, end);
int end){
//local variables
DrawSquare
stack frame
main() book-keeping
int i (DrawSquare arg)
...
14
Higher address
Call Stack
Lower address
int end){
DrawSquare
stack frame
main() book-keeping
int i (DrawSquare arg)
...
}
DrawLine
DrawSquare book-keeping
stack
frame
DrawLine(start, end);
//local variables
Top of Stack
15
Higher address
Call Stack
Lower address
DrawSquare
stack frame
...
}
main() book-keeping
16
Higher address
Call Stack
Lower address
Top of Stack
DrawSquare
stack frame
...
main() book-keeping
int i (DrawSquare arg)
}
17
Higher address
Call Stack
Lower address
int end){
//local variables
...
Top of Stack
18
Higher address
Today
Computer Memory
Pointers/Addresses
Arrays
Memory Allocation
19
Addresses
&X =
Address of X
Image by MIT OpenCourseWare.
21
Pointers
22
Pointers
23
Description
Character or small
integer.
Size*
1byte
Range*
signed: -128 to 127
unsigned: 0 to 255
2bytes
int
Integer.
4bytes
Long integer.
4bytes
bool
true or false
float
double
Double precision
8bytes
floating point number.
long double
25
Pointer Arithmetic
ptr
a
26
Pointer Arithmetic
ptr + 1
27
Pointer Arithmetic
ptr + 1
28
Pointer Arithmetic
Can do math on pointers
p1 = p2: sets p1 to the same address as p2
Addition/subtraction:
p1 + c , p1 - c
Increment/decrement:
p1++, p1--
29
30
Today
Computer Memory
Pointers/Addresses
Arrays
Memory Allocation
31
32
data[0] = 0;
data[1] = 1;
data[2] = 2;
33
char *ptr;
char str[10];
C-Style Strings
No string data type in C. Instead, a string is
\0
C-Style Strings
\0
36
String functionalities
#include <string.h>
char pointer arguments: char str1[14]
char* strcpy(char* dest, const char* source);
strcpy(str1, hakuna );
More in documentation
37
Today
Computer Memory
Pointers/Addresses
Arrays and Strings
Memory Allocation
38
Dynamic Allocation
#include <stdlib.h>
sizeof (a C language keyword) returns number
of bytes of a data type.
malloc/realloc finds a specified amount of free
memory and returns a void pointer to it.
char * str = (char *) malloc( 3 * sizeof(char) );
strcpy(str, hi);
strcpy(str, hello);
39
Dynamic Deallocation
#include <stdlib.h>
free declares the memory pointed to by a
strcpy(str, hi);
free(str);
40
declaration.
41
Summary
Memory has stack and heap.
Pointers and addresses access memory.
Arrays are really chunks of memory. Strings are
null-terminated char arrays.
C allows user memory allocation. Use malloc,
42
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.