-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicMemory.cpp
More file actions
60 lines (47 loc) · 1.38 KB
/
DynamicMemory.cpp
File metadata and controls
60 lines (47 loc) · 1.38 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
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
class cust
{
public:
int p;
};
int main()
{
// All Types of dynamic memory allocation
// pointer-variable = new data-type;
// Pointer initialized with NULL
// Then request memory for the variable
int *p = NULL;
p = new int;
// Combine declaration of pointer
// and their assignment
int *p = new int;
// pointer-variable = new data-type(value);
int *p = new int(25);
float *q = new float(75.25);
// Custom data type
// Works fine, doesn’t require constructor
cust *var1 = new cust;
// Works fine, doesn’t require constructor
cust *var2 = new cust();
// Notice error if you comment this line
//cust *var = new cust(25);
//Allocate a block of memory: new operator is also used to allocate a block(an array) of memory of type data-type.
//pointer-variable = new data-type[size];
int *p = new int[10];
/*delete operator
Since it is the programmer’s responsibility to deallocate dynamically allocated memory, programmers are provided delete operator in C++ language.
Syntax:
// Release memory pointed by pointer-variable
delete pointer-variable; */
delete p;
delete q;
// Release block of memory
// pointed by pointer-variable
//delete[] pointer-variable;
Example:
// It will free the entire array
// pointed by p.
delete[] p;
return 0;
}