-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicMemory.cpp
More file actions
48 lines (31 loc) · 1.06 KB
/
DynamicMemory.cpp
File metadata and controls
48 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
int main()
{
//dynamic memory = memory that is allocated after the program is already
// compiled & running. Use the 'new' operator to allocate memory
// in the heap rather than the stack
// Useful when we do not know how much memory we will need.
// Makes our programs more flexible especially when accepting user input.
int *pNum = NULL;
pNum = new int; //frees up room in the heap for one int (kinda like mallac)
*pNum = 21;
std::cout << "address: " << pNum << '\n';
std::cout << "Value: " << *pNum << '\n';
delete pNum; //like free
char *pGrades = NULL;
int size;
std::cout << "How many grades to enter in?: ";
std::cin >> size;
pGrades = new char[size];
for(int i = 0; i < size; i++)
{
std::cout << "Enter grade #" << i + 1 << ": ";
std::cin >> pGrades[i];
}
for(int i = 0; i < size; i++)
{
std::cout << pGrades[i] << " ";
}
delete[] pGrades;
return 0;
}