Dynamic Memory

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/6

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

7 Terms

1
New cards

Dynamic Memory

used to allocate elements in data structures

use pointers between dynamic instances to organise our data structure

2
New cards

Building a Dynamic ‘Singly Linked’ List

we can start with ‘no items’ 

and build up our data structure one item at a time

3
New cards

Chaining Nodes / Growing the List

e.g. Allocate a new node (using malloc)

‘chain it’ so our first item points to the new item

4
New cards

General Rules

allocate space for a ‘node’ (a struct) of the appropriate type

find where to add our item (start, end, insertion point)

adjust the pointers to stay consistent with the type of data structure we’re working with

5
New cards

In Memory

  • as the data structure grows, we allocate more ‘blocks’ of dynamic memory

  • we chain these together to form our data structure

  • we need to be careful to manage our pointers and hand memory back to the memory allocator

<ul><li><p>as the data structure grows, we allocate more ‘blocks’ of dynamic memory</p></li><li><p>we chain these together to form our data structure</p></li><li><p>we need to be careful to manage our pointers and hand memory back to the memory allocator</p></li></ul><p></p>
6
New cards

Declaring a ‘node’ struct in C

//define listItem node type
struct listItem {
char name [20];
char college [20];

//self-referential pointer
struct listItem *next; 
};

7
New cards

Declaring a variable (head pointer) in C

int main()
{
struct listItem *head = NULL;
}