-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
58 lines (47 loc) · 1.27 KB
/
memory.cpp
File metadata and controls
58 lines (47 loc) · 1.27 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
// Online C++ compiler to run C++ program online
//https://www.interviewbit.com/online-cpp-compiler/
#include <iostream>
#include <memory>
struct Point {
Point(int x1, int y1) : x(x1), y(y1){}
int x;
int y;
};
struct Free {
void operator()(int *p) {
free(p) ;
std::cout << "\nFree class: pointer freed\n" ;
}
};
void FreeFunc(int* p) {
free(p);
std::cout << "\nFreeFunc(): pointer freed\n";
}
int main() {
using namespace std;
{
std::unique_ptr<Point> uPt(new Point(4,6));
auto [x,y] = *uPt; //<-- structured bindings
cout << "\nx: " << x << " y: " << y;
uPt.reset(new Point(400, 600));
auto [x1,y1] = *uPt;
cout << "\nx1: " << x1 << " y1: " << y1;
cout << "\nraw pointer: " << uPt.get();
auto p = std::make_unique<int>(5) ;
auto pt = std::make_unique<Point>(3,5) ;
auto pArr = std::make_unique<int[]>(5) ;
pArr[0] = 100;
}
{
auto p = std::make_shared<int>(5) ;
auto pt = std::make_shared<Point>(3,5) ;
auto pArr = std::make_shared<int[]>(5) ;
pArr[0] = 0 ;
}
{
std::unique_ptr<int, Free> p1 {(int*) malloc(4), Free{}} ;
std::unique_ptr<int, void (*)(int *)> p2 {(int*) malloc(4), FreeFunc} ;
}
//other types are: shared_ptr, weak_ptr
return 0;
}