-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_mem.cpp
More file actions
39 lines (32 loc) · 889 Bytes
/
smart_mem.cpp
File metadata and controls
39 lines (32 loc) · 889 Bytes
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
#include <bits/stdc++.h>
template <typename T>
concept TriviallyCopyable = std::is_trivially_copyable<T>::value;
template <TriviallyCopyable T>
void safe_memcpy(T* dest, const T* src, size_t count)
{
if (dest == nullptr || src == nullptr)
{
throw std::invalid_argument("Null pointer passed to safe_memcpy");
}
std::memcpy(dest, src, count * sizeof(T));
}
struct MyType
{
int m_val;
MyType () = default;
MyType(int val): m_val(val)
{}
};
int main()
{
int src[5] = {1, 2, 3, 4, 5};
int dest[5];
safe_memcpy(dest, src, 5);
std::cout<< "destination arr: "<< dest[0]<<", "<< std::addressof(dest)<<"\n";
auto* memory = std::malloc(sizeof(MyType));
auto* my_type_ptr = reinterpret_cast<MyType*>(memory);
std::construct_at(my_type_ptr, MyType(1));
std::destroy_at(my_type_ptr);
std::free(memory);
return 0;
}