ICT1002 W11 LEC Dynamic Memory Allocation
ICT1002 W11 LEC Dynamic Memory Allocation
ALLOCATION
DR FRANK GUAN
ICT1002 – PROGRAMMING FUNDAMENTALS
WEEK 11
SELF-ASSESSMENT FOR WEEK 10
2
Agenda
1. Dynamic memory allocation
2. Linked lists
3
RECOMMENDED READING
Paul Deitel and
Harvey Deitel, C:
How to Program,
8th Edition,
Prentice Hall,
2016
– Chapter 12: C
Data Structures
4
DYNAMIC MEMORY ALLOCATION
5
WHY DYNAMIC MEMORY ALLOCATION?
include <stdio.h>
NUM_STUDENTS has to
#define NUM_STUDENTS 10
be defined at compile
int main() { time
int grades[NUM_STUDENTS];
int i;
7
MEMORY ALLOCATION
1. include #include <stdlib.h>
2. malloc int *ptr = (int *)malloc(sizeof(int)*N);
3. free
needed.
free(ptr);
9
MALLOC AND CALLOC
malloc allocates a block of memory with a
given number of bytes
int *ptr = (int *)malloc(sizeof(int) * N);
malloc VS calloc
https://stackoverflow.com/questions/1538420/difference-between-malloc-and-calloc
10
MALLOC AND CALLOC
malloc and calloc return a void pointer
to the start of the allocated memory
• it must be cast to the appropriate type before
use
11
THE SIZEOF OPERATOR
To be able to allocate memory dynamically,
we need to tell the compiler the size of
memory we need at runtime.
12
THE SIZEOF OPERATOR
int size = sizeof(int) * 4;
printf("size of 4 integers is:
%d bytes\n", size);
13
THE SIZEOF OPERATOR
int size = sizeof(int) * 4;
printf("size of 4 integers is:
%d bytes\n", size);
Output:
size of 4 integers is: 16 bytes
14
THE SIZEOF OPERATOR
The sizeof operator can also be used with
user-defined types:
typedef struct {
int id;
char name[25];
} Student;
printf("The size of a Student record is:
%d bytes\n", sizeof(Student));
15
FREE
free de-allocates memory previously
allocated by malloc or calloc
• all memory allocated by malloc or calloc
should eventually be free’d
16
EXAMPLE
#include <stdio.h>
#include <stdlib.h>
With dynamic memory
int main() { allocation, we can set the
int *grades;
number of students at run
int num_students; time.
/* ask how many grades need to be stored */
printf("How many students are in your class? ");
scanf("%d", &num_students);
/* allocate enough space to hold num_students integers */
grades = (int *)malloc(num_students * sizeof(int));
if (grades == NULL) {
printf("Out of memory.");
return 1;
}
/* de‐allocate memory */
free(grades);
/* copy the name into the array */
int length = strchr(buf, '\n') ‐ buf;
names[i] = (char *)calloc(length + 1, sizeof(char));
if (names[i] == NULL) {
printf("Out of memory.");
return 1;
}
strncpy(names[i], buf, length); Allocate space for each
} string according to its
length.
/* de‐allocate memory */
for (i = 0; i < num_students; i++)
free(names[i]);
free(names);
12 13 5 7 X
head
20
Arrays need
contiguous memory
slots. With a linked list,
you can optimise
Array memory by linking data
at different memory
locations.
Why are you teaching
us Linked Lists? We
are happy with
arrays!
Linked list
21
SELF-REFERENTIAL
STRUCTURES A self-
referential
structure
typedef struct node_struct {
int data; contains a
struct node_struct *next;
} Node;
pointer
member that
points to a
structure of the
same type
22
SELF-REFERENTIAL
STRUCTURES Self-referential
structures can
be linked
together to
form useful
data structures
such as linked
lists, queues,
stacks and
trees.
23
How do you create two nodes and
link node1 to node2?
node1 node2
24
CREATING A LINKED LIST
int main() {
Node node1 = { 15, NULL };
Node node2 = { 10, NULL };
node1.next = &node2;
node1 node2
25
ACCESSING DATA IN A LINKED LIST
int main() {
Node node1 = { 15, NULL };
Node node2 = { 10, NULL };
node1.next = &node2;
printf("node1.data = %d\n", node1.data);
printf("node1.next = %p\n", node1.next);
Node node1 = { 15, NULL };
Node node2 = { 10, NULL };
node1.next = &node2;
Node *node_ptr = &node1;
printf("node1.data = %d\n", node_ptr‐>data);
node_ptr = node_ptr‐>next;
printf("node2.data = %d\n", node_ptr‐>data);
}
Use the arrow
“‐>” operator for
pointer variables
27
ACCESSING DATA IN A LINKED LIST
12 13 5 7 X
Subsequent nodes are
head accessed via the link in By convention: the
each node. link pointer in the
A linked list is accessed last node points to
via a pointer to the first NULL.
node (head).
28
LINKED LIST ARRAY
Dynamic STATIC
The length of the list can increase or The size of an array cannot be altered once
decrease as necessary. memory is allocated.
29
LINKED LIST OPERATIONS
Search/update
Insert
Delete
30
LINKED LIST OPERATIONS Search
12 13 5 7 X
head temp
31
LINKED LIST OPERATIONS Search
12 13 5 7 X
head temp
temp = temp‐>next;
12 13 5 7 X
head temp
temp = temp‐>next;
temp = temp‐>next;
new_node head
Node *new_node = (Node *)malloc(sizeof(Node));
new_node‐>data = 100;
new_node‐>next = NULL;
new_node head
new_node‐>next = head;
35
LINKED LIST OPERATIONS
Insert
100 12 13 5 7 X
head
new_node‐>next = head;
head = new_node;
36
LINKED LIST OPERATIONS Delete
12 13 5 7 X
head node_ptr
Node *temp = head;
while (temp‐>next != NULL){
if (temp‐>next == node_ptr){
return temp;
}
temp = temp‐>next;
}
39
EXERCISE - 1
Write a function:
Node *search_list(Node *head, int target);
40
EXERCISE - 2
Write a function:
Node *insert_at_head(Node *head,
Node *new_node);
41
EXERCISE - 3
Write a function:
Node *delete_node(Node *head, Node
*node_ptr);
43
END-OF-WEEK CHECKLIST
44
ABOUT CLASS TEST
– Date: 21 Nov 2019 (Thursday)
– Time:
– Seated before 9:00AM
– Start punctually at 9:15AM
– Duration: 45 minutes
– Scope
– Week 8 – Week 11
– Note:
– You are not allowed bring any notes into the test.
– You are not allowed to use any electronic devices.
– You may bring in your calculator.
45