pointer
pointer
address)
memory location address
p
129 10000
pointer 10004 77 10001
31542 10002
dereferencing:
108 10003
*
97 10004
542 10005
100652 10006
1
C pointers
2
Declaring pointer variables
*(&p) p always
4
Arrays
More arrays
6
More arrays
x x
p p
Uninitialized pointers
the pointer has not been initialized to point to a valid
location
Dangling pointers
the pointer points at a memory location that has actually
been deallocated
10
Background: Runtime Memory Organization
stack
(grows downwards)
heap
(grows upwards)
global data
0x00000000
11
stack growth
}
top of
stack
}
}
12
Background: Runtime Memory Organization
Code: Runtime stack:
stack growth
}
top of
stack
}
}
13
stack growth
}
}
} top of
stack
14
Background: Runtime Memory Organization
Code: Runtime stack:
stack growth
}
}
}
{
top of
stack
}
15
stack growth
}
}
} top of
stack
stack frame
{ (deallocated)
16
Background: Runtime Memory Organization
Code: Runtime stack:
stack growth
}
top of
stack
} stack frame
stack frame
{
17
stack growth
}
} stack frame
18
Structs
A struct is
an aggregate data structure, i.e., a collection of other
data;
19
Declaring structs
next
struct node {
int val;
struct node *next;
}
Given Given
a struct s containing a a pointer p to a struct s
field f containing a field f
to access f, we write to access f we write
s.f declares x, y to be p->f // eqvt. to: (*p).f
variables of type
Example: struct foo
Example:
struct foo { struct foo {
int count, bar[10];
int count, bar[10];
} *p, *q;
} x, y;
21
23
24