0% found this document useful (0 votes)
25 views

Lect-06-Memory Allocation

c++

Uploaded by

wwerazo8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lect-06-Memory Allocation

c++

Uploaded by

wwerazo8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer Language II

Lecture 6: Memory Management

Dr. Khari Armih

[email protected]

June 24, 2023

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 1 / 14


Outline
❶ Memory Allocation
❷ Static Allocation
❸ Dynamic Allocation

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 2 / 14


Static Arrays
• Special variable
– group of variables have one name
• Declaration:
type name[int]
• Array can be any type (int, float, char ......)
• N elements, numbered from 0 to N-1

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 3 / 14


Static Arrays
• The Size can be given as:
– non-negative integer, or
– an expression that evaluates to a non-negative integer
• e.g. int a[6]; - allocates 6 * 4 == 24 bytes
• e.g. char b[6]; - allocates 6 * 1 == 6 bytes
• e.g. double c[6]; - allocates 6 * 8 == 48 bytes

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 4 / 14


Memory Allocation
• Good programmers make efficient use of memory
• Understanding memory allocation is important
– Create data structures of arbitrary size
– Avoid ”memory leaks”
– Run-time performance

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 5 / 14


Memory Allocation (cont.)
• Static Allocation (fixed in size)
– Sometimes we create data structures that are ”fixed” and don’t
need to grow or shrink
• Dynamic Allocation (change in size)
– At other times, we want to increase and decrease the size of our
data structures to accommodate changing needs
• Often, real world problems mean that we don’t know how much
space to declare, as the number needed will change over time.

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 6 / 14


Static Allocation
• Done at compile time
• Global variables: variables declared ”ahead of time” such as fixed
arrays
• Lifetime = entire runtime of program
• Advantage: efficient execution time
• Disadvantage:
– If we declare more static data space than we need, we waste space
– If we declare less static space than we need, we are out of luck

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 7 / 14


Dynamic Allocation
• Done at run time
• Data structures can grow and shrink to fit changing data
requirements
– We can allocate (create) additional storage whenever we need them
– We can de-allocate (free/delete) dynamic space when ever we are
done with them
• Advantage: we can always have exactly the amount of space
required - no more, no less
• For example, we can use dynamic data structures to create a chain
of data structures called a linked list

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 8 / 14


Allocating Single Object
• C++ uses the ”new” operator to dynamically allocate memory
• The ”new” operator returns the address to the start of the
allocated block
• You use the new operator to allocate a memory location as follows:

<pointer-variable> = new <Data-type>;


• Where:
– <Data-type> is any valid data type
– <pointer-variable> is a pointer variable to hold the address
of a variable with <Data-type>

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 9 / 14


Allocating Single Object (cont.)
• C++ uses the ”delete” operator to free memory allocated with
new operator
• The delete operator should be called on a pointer to dynamically
allocated memory when it is no longer needed
• We use the delete operator to allocate a memory location as
follows:

delete <pointer-variable>;
• After delete is called on a memory region, that region should no
longer be accessed by the program

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 10 / 14


Full Example
1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5 // Pointer initialized with null
6 double * p = NULL;
7 // Request memory for the variable
8 p = new double ;
9 // Store value at allocated address
10 *p = 999.99;
11 cout << "Value of pvalue : " << *p << endl;
12 // free up the memory.
13 delete p;
14 return 0;
15 }
Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 11 / 14
Dynamically Allocating Arrays
• You can allocate an array of values as following:

<pointer-variable> = new <Data-type>[size];


• Example:

int *array = new int[100];


• The delete operator frees memory previously allocated. For
arrays, you need to include empty brackets, as in

delete[] array;

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 12 / 14


Full Example
1 #include<iostream>
2 using namespace std;
3 void printArray(int *p, int s)
4 {
5 for(int i=0; i<s; i++)
6 {
7 cout << p[i];
8 }
9 }
10 int main()
11 {
12 int n;
13 cout << "Enter array size: ";
14 cin >> n;
15 // Request memory for the variable
16 int *array = new int[n];
17 for(int i=0; i<n; i++)
18 {
19 array[i] = i;
20 }
21 printArray(array,n);
22 return 0;
23 }
Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 13 / 14
lab 1
• Write a function createIndexArray(n) that returns an integer
array of size n in which each element is initialized to its index.
As an example, calling
int * array = createIndexArray(20)
• Write a function that create sub array
int *subArray(int *array, int start, int end)

Dr. Khari Armih ([email protected]) Computer Language II June 24, 2023 14 / 14

You might also like