Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Assumptions
Audience familiar with: Variables Control structures Function calls Arrays and strings Not familiar with
Computer memory
function calls
2
Intro to Pointers and Memory in C
Topics
Computer memory in general
Implementation of variables and function
calls Pointers and overcoming function limitations Pointer arithmetic and arrays Need and risks of dynamic memory allocation
3
Intro to Pointers and Memory in C
Focus
Understanding of concepts and
techniques Reasoning for use of specific techniques Caveats to watch out for
Note on C versions
Presented material is in C90 (old
Ansi C) Supported by nearly all compilers Basis for most legacy code
Newer C99 and later is different
Allows some C++ conventions Introduces new features Not fully supported by all compilers
5
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
Background
Pointers
Dynamic
stored value Cant rely on actual value Programmers see linear address space Typical address space is vast 32bit or 64bit
8
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
Spread values in contiguous cells Only need to know starting address and
size
Background
Pointers
Dynamic
10
Background
Pointers
Dynamic
Virtual Memory
Modern OSs provide separate address space
to processes Certain logical addresses mapped to physical addresses Mechanism transparent Overlapping addresses do not interfere Not every address is mapped!
11
Background
Pointers
Dynamic
12
Background
Pointers
Dynamic
Variables
A symbolic name for a specific memory
location Can read and write contents Guaranteed to exist every time it is used Type used to reassemble and interpret value How many cells? How value is broken down 13
13
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
Automatic variables
Declared at a beginning of a block Lifetime and scope correspond to block
14
Background
Pointers
Dynamic
Newer variables allocated above earlier Newer variables die before older
Stack resides in specific memory segment
15
Background
Pointers
Dynamic
IP changes on branches
16
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
17
Background
Pointers
Dynamic
18
Background
Pointers
Dynamic
into stack Return address pushed into stack CPU IP aimed at beginning of function block
19
Background
Pointers
Dynamic
20
Background
Pointers
Dynamic
All passed arguments are popped IP aimed at next instruction to execute in original
21
Background
Pointers
Dynamic
22
Background
Pointers
Dynamic
23
Background
Pointers
Dynamic
24
Background
Pointers
Dynamic
25
Background
Pointers
Dynamic
26
Background
Pointers
Dynamic
Pointers
27
Background
Pointers
Dynamic
Why pass-by-pointer?
Functions always pass values by copying (pass-by-
value) They return a single value by copying Changes to arguments in invoked functions have NO IMPACT on corresponding variables in invoking function To change, we must break through to earlier stack locations.
Solution: 1) Pass address of value holder represented by variable 2) Use that address to write directly into the value
28
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
Pointers
A pointer is a variable! It has an address and stores a value
But the value can be interpreted as an
address X myX declare variable of type X X* pX declare pointer to value of type X Pointer size depends on address space, not type!
29
Background
Pointers
Dynamic
Arbitrary numeric values Address of variables Values of other pointers Address of functions (outside our scope) Caveat: Avoid pointing at temporaries!
30
Background
Pointers
Dynamic
Dereferencing pointers
31
Background
Pointers
Dynamic
Implementing Swap
32
Background
Pointers
Dynamic
Implementing Swap
33
Background
Pointers
Dynamic
Implementing Swap
34
Background
Pointers
Dynamic
Implementing Swap
35
Background
Pointers
Dynamic
etc.
36
Background
Pointers
Dynamic
dereferenced Equivalent to accessing address 0 Access may result in Bus Error on UNIX
37
Background
Pointers
Dynamic
vars
blocks
38
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
Casting pointers
39
Background
Pointers
Dynamic
Void*
Pointer type used to represent addresses without any type information Shouldnt be dereferenced Programmer responsible for coercion
40
Background
Pointers
Dynamic
Const pointers
41
Background
Pointers
Dynamic
Pointers to pointers
42
Background
Pointers
Dynamic
Pointers to pointers
43
Background
Pointers
Dynamic
Pointers to pointers
44
Background
Pointers
Dynamic
Pointers to pointers
45
Background
Pointers
Dynamic
Pointer Arithmetic
C permits addition/subtraction on pointers Increments correspond to size of type value
Effective with contiguous set of variables
46
Background
Pointers
Dynamic
47
Background
Pointers
Dynamic
for assignment to pointer Passable to function that takes pointer or array Not reassignable Sizeof(ar) returns array size
48
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
49
Background
Pointers
Dynamic
50
Background
Pointers
Dynamic
51
Background
Pointers
Dynamic
time
Live after function ends
Die before program terminates
52
Background
Pointers
Dynamic
given size. Size often specified as n*sizeof(type) If failed, returns NULL If valid, returns void pointer to new memory area Programmer converts into pointer to first element Pointer arithmetic or subscripts access rest of space No bounds checking!
NULL otherwise
53
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
The heap
A memory area provided to the process for allocating
data Often limited Runtime tracks where all memory is allocated Every allocation is contiguous Possibility of fragmentation even if enough total free space No way to check in advance
54
Background
Pointers
Dynamic
55
Background
Pointers
Dynamic
Malloc/Free example
56
Background
Pointers
Dynamic
free() caveats
Do not free a pointer prematurely
Make sure it will not be accessed again via
57
Background
Pointers
Dynamic
free() caveats
Once pointer to memory is lost, no way to free
58
Background
Pointers
Dynamic
free() caveats
Freeing the same memory twice Freeing an automatic variable
59
Background
Pointers
Dynamic
pointers Storage of large values Implementation of variables and calls Lifetime and effect on pointer validity Need for pointers Pointers and their risks Pointer arithmetic and relation to arrays Need and risk of dynamic allocation
60
Intro to Pointers and Memory in C
Background
Pointers
Dynamic
Questions?
61
Backup materials
62
63
64
65
66
Null-terminated strings
Strings: Array of chars terminated by \0 or 0.
String can take less space than actual array size
Since \0 can come early Missing the \0 will lead many functions past allocated
67
Global Variables
Variables can be declared outside functions Accessible from everywhere Initialized before main() started Live until program terminates Function static variables can be thought of as global
68