03 Mem Linkedlist 1254
03 Mem Linkedlist 1254
Memory
Pointers
Linked lists
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 1
Announcements
• IMPORTANT!
▪ Please do not post full or partial solutions (correct or otherwise) to graded activities (labs, HW, PA, exams) in
public Piazza posts
▪ If you must share code (but we encourage first careful inspection of your own logic, expected output vs
actual output), you can make a private Piazza post to instructors
• HW1 due Tuesday May 20, 22:00 Coding practice activity released
Will be unavailable during exams
• PA1 released, due Saturday May 24, 22:00
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 2
C++ and memory
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 3
Calling functions in C++
Parameters
Formal parameters
Actual parameters
• Actual parameter
▪ Value(s) or variable(s) specified by the function caller
• Formal parameter
▪ Variables found in the signature/header of the function itself
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 4
Function parameters
• Unless written otherwise, (most) parameters in C++ are passed by value ("call-by-value")
▪ the value of the actual parameter is copied to the formal parameter when the function is called
• The actual parameters and formal parameters are different variables in memory, even if they are
named the same
• If you change the value of the formal parameter, this does not affect the value of the actual
parameter back in the caller's memory
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 5
Function parameters and the call stack
• Example
// ... double circleArea(double radius){
int r = 3; double pi = 3.1415;
double area = circleArea(r); double sq_r = square(radius);
// ... return sq_r * pi;
}
double square(double x){
return x * x;
}
main memory
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 6
Function parameters and the call stack
• Example
// ... double circleArea(double radius){
int r = 3; double pi = 3.1415;
double area = circleArea(r); double sq_r = square(radius);
// ... return sq_r * pi;
}
double square(double x){
return x * x;
}
main memory
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 7
Function parameters and the call stack
• Example
// ... double circleArea(double radius){
int r = 3; double pi = 3.1415;
double area = circleArea(r); double sq_r = square(radius);
// ... return sq_r * pi;
}
double square(double x){
return x * x;
}
main memory
3 28.274
r area
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 8
Call-by-value problems
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 9
Dynamic memory
aka heap memory
• Variables declared in a function only exist within the scope of that function (or code block
enclosed by { } )
• The data structures we will learn about in this course require objects and variables to persist
beyond a function's lifetime
▪ cannot be allocated to call stack, need to put somewhere else
▪ heap memory / dynamic memory
• We still need local variables that refer or point to the dynamically allocated memory
▪ In C++ such variables are pointers
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 10
Addresses and pointers
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 11
Declaring pointers
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 12
Address operator and dereferencing
p x
… 4096 … 23
0 1 212 220-1
int x = 23;
int* p = &x;
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 13
Address operator and dereferencing
p x
… 4096 … 47
23
0 1 212 220-1
int x = 23;
int* p = &x;
x = 47;
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 14
Address operator and dereferencing
p x
… 4096 … 47
38
0 1 212 220-1
int x = 23;
int* p = &x;
x = 47;
*p = 38;
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 15
Pointers as parameters
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 16
Pointer to a pointer
...to a pointer to a pointer...
int main() {
int x = 5;
int* p = &x;
*p = 6;
int** q = &p;
int*** r = &q;
• "You can keep adding levels of pointers until your brain explodes or
the compiler melts – whichever happens soonest"
▪ stackoverflow user JeremyP
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 17
Type compatibility for pointer assignments
int a = 5;
int* b = new int;
int* c = &a; allocates an array of size a
*c = 4; in dynamic memory (heap)
int** d = &b;
int* e = new int[a];
**d = 3;
int* f = new int[*b];
delete b;
delete e; // causes a memory leak
delete[] f;
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 19
Dangling pointers
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 20
Memory leaks
int* arr;
int sz = 4;
arr = new int[sz];
arr[2] = 5;
arr = new int[sz];
arr[2] = 7;
stack heap
Lost access to
5 this array!
4
arr sz
7
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 21
Linked lists
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 22
Here's something new
and it doesn't have to be scary!
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 23
Linked list nodes
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 24
Node pointers
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 25
Linked lists
• A linked list is a chain of nodes where each node stores the address of the next node
Start
7 2 6 8
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 26
Linked list implementation
Node class
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 27
Building a linked list
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 28
Building a linked list
7 3
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 29
Traversing a linked list
7 3
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 30
Take a break!
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 31
Linked list insertion
15 16 19 22 28 …
p 17
b
Important!
Node<int>* b = new Node<int>(17, p->next); Be aware that sequential nodes are
not guaranteed to be found in
p->next = b;
sequential memory locations!
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 32
Linked list removal
15 16 19 22 28 …
p
b
p->next = b->next;
delete b;
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 33
Linked lists, by features
We will come back to this slide later
• Linkage • Termination • Access/entry
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 34
Some linked list variations
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 35
Singly-linked list with tail pointer
Insertion at back of list
head 1 2 3 4 6 7
tail
newnode
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 36
Doubly-linked list
// constructors, etc.
};
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 37
Doubly-linked list insertion
head
6 12 4 23
curr
temp 7
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 38
Doubly-linked list removal
head
6 12 7 4 23
curr
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 39
Linked list variations
Termination variants – Circular, sentinels
• Circular singly-linked list • Sentinel nodes
▪ “dummy” nodes at ends of lists which do not
…
contain any data
▪ eliminate special cases for list modifications
• e.g. insert/remove first/last node, all cases
implemented the same way
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 40
Linked lists are recursive!
• Iteration is convenient for many list operations (e.g. ordinary traversal, insertion, removal, etc.)
▪ but can be costly for other operations
• Consider printing the contents of a singly-linked list in reverse
head 12 25 3 44 18
}
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 41
Something slightly different
head 12 25 3 44 60 18
2025S } Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 42
Something challenging!
head 12 25 3 44 18
head 18 44 3 25 12
}
return ;
}
2025S Will Evans / Steve Wolfman / Cinda Heeren / Jordon Johnson / Seva Lynov / Geoffrey Tien 43