-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
22 lines (20 loc) · 1.13 KB
/
memory.cpp
File metadata and controls
22 lines (20 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> // for output
#include <memory> // for smart pointers
int main() {
std::unique_ptr p = std::make_unique <int> (5); // to make array do: std::unique_ptr ptr = std::make_unique <char[]> (size of array);
std::unique_ptr <int> ptr(new int(10)); // to make array do: std::unique_ptr <char[]> ptr(new char[size of array]{"example"});
std::shared_ptr point = std::make_shared <int> (15); // same as above, just change names to shared_ptr
std::shared_ptr <int> pointer(new int(20)); // same as above, just change names to shared_ptr
int *pointers = (int*)malloc(sizeof(int)); // by default is 0, it is works with '(type)' and with `static_cast<type>`
*pointers = 25;
int *pointerito = new int(30); // if you do 'new int' it will be 0 by default
std::cout << *p << std::endl;
std::cout << *ptr << std::endl;
std::cout << *point << std::endl;
std::cout << *pointer << std::endl;
std::cout << *pointers << std::endl;
std::cout << *pointerito << std::endl;
free(pointers); // no memory leak
delete(pointerito); // no memory leak
// if was array you needed to do: delete []arr
}