Download presentation
Presentation is loading. Please wait.
Published byEvan Lynch Modified over 9 years ago
1
Computer Science 210 Computer Organization Pointers and Dynamic Storage
2
Heap Allocation and Deallocation The C programmer must run functions to allocate and deallocate memory for dynamic storage (referenced by pointers) malloc allocates a block of storage from the heap free returns storage to the heap
3
int *intPtr; Declare a Pointer to an int intPtr The variable is uninitialized and contains garbage, but it can point to an int eventually
4
#include int *intPtr = NULL; // NULL is the same as 0 Declare and Initialize to NULL 0 intPtr The variable is initialized to the NULL pointer, which can now be accessed
5
#include int *intPtr = malloc(sizeof(int)); Declare and Initialize with malloc intPtr The variable now points to a new storage area for an int, but this storage contains garbage malloc expects an integer representing the size of the storage needed The sizeof function takes a type as an argument and returns the number of bytes needed for a value of that type malloc returns a pointer to the first byte of the storage
6
#include int *intPtr = malloc(sizeof(int)); (*intPtr) = 69; printf("%d\n", *intPtr); Access via Dereference with * intPtr The variable now points to storage that has a program-supplied value 69 The * operator accesses the cell pointed to The pointer must point to a cell * has a lower precedence than =
7
#include int *intPtr = malloc(sizeof(int)); (*intPtr) = 69; printf("%d\n", *intPtr); free(intPtr); Return the Storage with free intPtr The variable still points to storage, which might be used by others 69 The storage is still accessible, but might be taken for other applications (also called a dangling pointer reference)
8
#include int *intPtr = malloc(sizeof(int)); (*intPtr) = 69; printf("%d\n", *intPtr); free(intPtr); intPtr = NULL; Reset the Pointer to null 0 intPtr The variable now has no dangling pointer 69 Clean up by setting the pointer to NULL or to other storage
9
#include int *intPtr = malloc(sizeof(int)); (*intPtr) = 69; printf("%d\n", *intPtr); intPtr = NULL; Be Careful of Memory Leaks! 0 intPtr 69 In this case, the storage cannot be accessed by the program, or by the heap manager, and will eventually result in heap underflow! Always free your own storage!
10
Nodes and Linked Structures datanextdatanext 2 head size Could be a linked stack How do we represent a node in C?
11
The C struct A struct is a data type that includes one or more named data fields Good for containing data of different types Like classes in Python or Java, but without the methods
12
struct point{ int x, y; } p1, p2; Declare a Type and Two Variables p1 Storage is automatically allocated when variables are declared x x y p2 y
13
struct point{ int x, y; } p1, p2; p1.x = 44; p1.y = 55; Initialize the Fields of p1 p1 The field selector (. ) accesses a field for reference or assignment x 55 44 x y p2 y
14
struct point{ int x, y; } p1, p2; p1.x = 44; p1.y = 55; p2 = p1; Copy p1 to p2 55 p1 Unlike arrays, the contents of an entire struct can be assigned to another one 44 x 55 44 x y p2 y
15
struct point{ int x, y; }; struct point p1, p2; p1.x = 44; p1.y = 55; p2 = p1; Variation 55 p1 Can declare the type, then the variables later 44 x 55 44 x y p2 y
16
typedef struct point{ int x, y; } point; point p1, p2; p1.x = 44; p1.y = 55; p2 = p1; Create a Synonym with typedef 55 p1 This synonym happens to be the same name as the name of the struct 44 x 55 44 x y p2 y
17
typedef struct node{ int data; struct node *next; } node; typedef node* nodePtr; Declare a Node Type No storage yet, because no variables have been declared
18
typedef struct node{ int data; struct node *next; } node; typedef node* nodePtr; nodePtr ptr; Declare a Pointer Variable to a Node ptr ptr contains garbage
19
typedef struct node{ int data; struct node *next; } node; typedef node* nodePtr; nodePtr ptr = malloc(sizeof(node)); Initialize It to a New Node ptr malloc allocates storage for the entire node
20
typedef struct node{ int data; struct node *next; } node; typedef node* nodePtr; nodePtr ptr = malloc(sizeof(node)); ptr->data = 69; ptr->next = NULL; Initialize the Node’s Contents ptr The -> operator combines * (dereference) and. (field selection) 69
21
typedef struct node{ int data; struct node *next; } node; typedef node* nodePtr; nodePtr getNode(int data, nodePtr next){; nodePtr ptr = malloc(sizeof(node)); ptr->data = data; ptr->next = next; return ptr; } Define a Helper Function getNode This function creates a new node, initializes its contents, and returns a pointer to it
22
typedef struct node{ int data; struct node *next; } node; typedef node* nodePtr; nodePtr getNode(int data, nodePtr next){; nodePtr ptr = malloc(sizeof(node)); ptr->data = data; ptr->next = next; return ptr; } int i; nodePtr head = NULL; for (i = 1; i <= 3; i++) head = getNode(i, head); Create a Linked Structure! 31 head 2
23
typedef struct node{ int data; struct node *next; } node; typedef node* nodePtr; typedef struct linkedStack{ int size; nodePtr top; } linkedStack; linkedStack newStack(); void push(linkedStack *stk, int data); int pop(linkedStack *stk); int size(linkedStack stk); The Header File for a Linked Stack
24
linkedStack newStack(){ linkedStack stk; stk.top = NULL; stk.size = 0; return stk; } void push(linkedStack *stk, int data){ stk->top = getNode(data, stk->top); stk->size = stk->size + 1; } int pop(linkedStack *stk){ int data = stk->top->data; nodePtr garbage = stk->top; stk->top = stk->top->next; free(garbage); stk->size = stk->size - 1; return data; } int size(linkedStack stk){ return stk.size; } The Implementation
25
#include #include "stack.h" int main(){ linkedStack stk = newStack(); int i; for (i = 1; i <= 5; i++) push(&stk, i); printf("The stack has %d elements\n", size(stk)); while (size(stk) > 0) printf("Popping %d\n", pop(&stk)); printf("The stack has %d elements\n", size(stk)); } Using the Linked Stack
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.