Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects....
-
Upload
clemence-adams -
Category
Documents
-
view
223 -
download
0
Transcript of Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects....
![Page 1: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/1.jpg)
Dynamic MemoryDynamic Memory
![Page 2: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/2.jpg)
2
OverviewOverview
• Virtual Memory
• What is Dynamic Memory ?
• How to find the size of objects.
• Allocating memory.
• Deallocating memory.
![Page 3: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/3.jpg)
3
Virtual MemoryVirtual Memory
Text Segment
Data Segment
Stack segment
program instructions
static and dynamic data
local variables, parameters
free memory
![Page 4: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/4.jpg)
4
Virtual MemoryVirtual Memory
Text Segment
Static data
Heap
Stack segment
global variables, etc.
dynamic data
![Page 5: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/5.jpg)
5
Virtual MemoryVirtual Memory
Text Segment
Static data
Heap
Stack segment
memory is allocated and deallocated as needed
![Page 6: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/6.jpg)
6
What is Dynamic Memory?What is Dynamic Memory?
• Memory which is allocated and deallocated during the execution of the program.
• Types:
– data in run-time stack
– dynamic data in the heap
![Page 7: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/7.jpg)
7
Example: Run-Time StackExample: Run-Time Stack
•Memory is allocated when a program calls a function.
– parameters– local variables– where to go upon return
•Memory is deallocated when a program returns from a function.
![Page 8: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/8.jpg)
8
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stackstack
![Page 9: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/9.jpg)
9
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stack
3.15a
b
![Page 10: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/10.jpg)
10
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stack
3.15a
b
3.15x
![Page 11: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/11.jpg)
11
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stack
3.15a
b
3.15x
line 16
![Page 12: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/12.jpg)
12
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stack
3.15a
b
3.15x
line 16
result
![Page 13: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/13.jpg)
13
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stack
3.15a
b
3.15x
line 16
9.9225result
![Page 14: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/14.jpg)
14
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stack
3.15a
b
3.15x
line 16
9.9225result
![Page 15: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/15.jpg)
15
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
9.9225
stack
3.15a
b
![Page 16: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/16.jpg)
16
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
9.9225
stack
3.15a
b
![Page 17: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/17.jpg)
17
#include <stdio.h>
float square(float x){ float result;
result = x * x; return result;}
main(){ float a = 3.15; float b; b = square(a); printf(“%f\n”, b);}
01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:
stackstack
![Page 18: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/18.jpg)
18
#include <stdlib.h>#include <stdio.h>
int factorial (int x){
if (x == 0){
return 1;}return x * factorial (x –1);
}
void main(){
int n;printf(“Enter a number: \n”);scanf(“%d”, &n);
printf(“Factorial: %d\n”, factorial(n);
}
![Page 19: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/19.jpg)
19
How much memory to allocate?How much memory to allocate?
•The sizeof operator returns the size of an object, or type, in bytes.
•Usage:
sizeof(Type)
sizeof Object
![Page 20: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/20.jpg)
20
Example 1:
int n;char str[25];float x;double numbers[36];
printf(“%d\n”, sizeof(int));printf(“%d\n”, sizeof n);
n = sizeof str;n = sizeof x;n = sizeof(double);n = sizeof numbers;
![Page 21: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/21.jpg)
21
Notes on Notes on sizeofsizeof
•Do not assume the size of an object, or type; use sizeof instead.
•In DOS: 2 bytes (16 bits)•In GCC/Linux: 4 bytes (32 bits)•In MIPS: 4 bytes (32 bits)
Example: int n;
![Page 22: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/22.jpg)
22
#include <stdio.h>
#define MAXNAME 80#define MAXCLASS 100
struct StudentRec{ char name[MAXNAME]; float mark;};
typedef struct StudentRec Student;
Example 2:
![Page 23: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/23.jpg)
23
int main(){ int n; Student class[MAXCLASS];
n = sizeof(int); printf(“Size of int = %d\n”, n); n = sizeof(Student); printf(“Size of Student = %d\n”, n);
n = sizeof class; printf(“Size of array class = %d\n”, n);
return 0;}
Example 2 (cont.):
![Page 24: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/24.jpg)
24
Notes on Notes on sizeof sizeof (cont.)(cont.)•The size of a structure is not necessarily the sum of the sizes of its members.
Example: struct cardRec { char suit; int number;};
typedef struct cardRec Card;
Card hand[5];
printf(“%d\n”, sizeof(Card));printf(“%d\n”, sizeof hand);
“alignment” and “padding”
5*sizeof(Card)
![Page 25: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/25.jpg)
25
Dynamic Memory: HeapDynamic Memory: Heap
•Memory can be allocated for new objects.•Steps:
•determine how many bytes are needed•allocate enough bytes in the heap•take note of where it is (memory address)
![Page 26: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/26.jpg)
26
#include <stdlib.h>
main(){ int* aPtr = NULL; aPtr = (int*)malloc(sizeof(int)); *aPtr = 5;
free(aPtr);}
Example 1:
NULL
stack
aPtr
heap
![Page 27: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/27.jpg)
27
#include <stdlib.h>
main(){ int* aPtr = NULL; aPtr = (int*)malloc(sizeof(int)); *aPtr = 5;
free(aPtr);}
heap
0x3a04
NULL
stack
aPtr 0x3a04
Example 1:
![Page 28: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/28.jpg)
28
#include <stdlib.h>
main(){ int* aPtr = NULL; aPtr = (int*)malloc(sizeof(int)); *aPtr = 5;
free(aPtr);}
heap
0x3a04
NULL
stack
aPtr 0x3a04
“type cast”
Example 1:
![Page 29: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/29.jpg)
29
#include <stdlib.h>
main(){ int* aPtr = NULL; aPtr = (int*)malloc(sizeof(int)); *aPtr = 5;
free(aPtr);}
5
heap
0x3a04
stack
aPtr 0x3a04
Example 1:
![Page 30: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/30.jpg)
30
#include <stdlib.h>
main(){ int* aPtr = NULL; aPtr = (int*)malloc(sizeof(int)); *aPtr = 5;
free(aPtr);}
0x3a04
stack
aPtr
Example 1:
heap
![Page 31: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/31.jpg)
31
#include <stdlib.h>
main(){ int* aPtr = NULL; aPtr = (int*)malloc(sizeof(int)); *aPtr = 5;
free(aPtr);}
0x3a04
stack
aPtr
Example 1:
deallocates memory
heap
![Page 32: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/32.jpg)
32
#include <stdlib.h>
main(){ int* aPtr; int* bPtr;
aPtr = (int*)malloc(sizeof(int)); *aPtr = 5;
bPtr = (int*)malloc(sizeof(int)); *bPtr = 8;
free(aPtr);
aPtr = bPtr; bPtr = (int*)malloc(sizeof(int)); *bPtr = 6;}
Example 2:
![Page 33: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/33.jpg)
33
Allocating MemoryAllocating Memory
• NeedNeed to include stdlib.h• malloc(n) returns a pointer to n bytes of
memory.
• AlwaysAlways check if malloc has returned the NULL pointer.
• ApplyApply a type cast to the pointer returned by malloc.
![Page 34: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/34.jpg)
34
Deallocating MemoryDeallocating Memory
• free(pointer) deallocates the memory pointed to by a pointer.
• It does nothing if pointer == NULL.• pointer must point to memory previously
allocated by malloc.• ShouldShould free memory no longer being used.
![Page 35: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/35.jpg)
35
main(){ Student* studentPtr = NULL;
studentPtr = (Student*)malloc(sizeof(Student));
if (studentPtr == NULL) { fprintf(stderr, “Out of memory\n”); exit(1); }
*studentPtr = readStudent(); printStudent(*studentPtr);
free(studentPtr);}
Example 3:
![Page 36: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/36.jpg)
36
main(){ Student* class = NULL; int n, i, best = 0;
printf(“Enter number of Students: ”); scanf(“%d”, &n);
class = (Student*)malloc(n * sizeof(Student)); if (class != NULL) { for (i = 0; i < n; i++) { class[i] = readStudent(); if (class[best].mark < class[i].mark) { best = i; } } printf(“Best student: ”); printStudent(class[best]); }}
Example 4:
![Page 37: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/37.jpg)
37
Common ErrorsCommon Errors
• Assuming that the size of a structure is the sum of the sizes of its members.
• Referring to memory already freed.
• Not freeing memory which is no longer required.
• Freeing memory not allocated by malloc.
![Page 38: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/38.jpg)
38
#include <stdio.h>#include <stdlib.h>
float** makeMatrix(int n, int m){ float* memoryPtr; float** matrixPtr; int i;
memoryPtr = (float*)malloc(n*m*sizeof(float)); matrixPtr = (float**)malloc(n*sizeof(float*)); if (memoryPtr == NULL || matrixPtr == NULL) { fprintf(stderr, “Not enough memory\n”); exit(1); } for (i = 0; i < n; i++, memoryPtr += m){ matrixPtr[i] = memoryPtr; } return matrixPtr;}
Example 5:
![Page 39: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/39.jpg)
39
RevisionRevision
• sizeof
• malloc
• free
• Common errors.
PreparationPreparation
• Read Kruse et al. Chapter 4, Section 4.5
![Page 40: Dynamic Memory. 2 Overview Virtual Memory What is Dynamic Memory ? How to find the size of objects. Allocating memory. Deallocating memory.](https://reader035.fdocuments.us/reader035/viewer/2022062722/56649f325503460f94c4dfcc/html5/thumbnails/40.jpg)
40
NextNext
Nodes and Linked Structures