add Chapter02 examples

This commit is contained in:
Amar Mahmutbegovic
2024-04-27 21:02:06 +02:00
parent de6d74e093
commit 5af72efc42
6 changed files with 148 additions and 0 deletions

10
Chapter02/README.md Normal file
View File

@@ -0,0 +1,10 @@
# Chapter02
This folder contains examples in C++ from second chapter.
The folder contains following examples:
- vector_dyn_mem.cpp
- vector_dyn_mem_reserve.cpp
- vector_pmr.cpp
- terminate_handler.cpp
- new_deleted.cpp

21
Chapter02/new_deleted.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <cstdio>
#include <vector>
#include <new>
void *operator new(std::size_t count) = delete;
void *operator new[](std::size_t count) = delete;
void *operator new(std::size_t count, std::align_val_t al) = delete;
void *operator new[](std::size_t count, std::align_val_t al) = delete;
void *operator new(std::size_t count, const std::nothrow_t &tag) = delete;
void *operator new[](std::size_t count, const std::nothrow_t &tag) = delete;
void *operator new(std::size_t count, std::align_val_t al, const std::nothrow_t &) = delete;
void *operator new[](std::size_t count, std::align_val_t al, const std::nothrow_t &) = delete;
int main() {
std::vector<int> vec;
vec.push_back(123);
printf("vec[0] = %d\r\n", vec[0]);
return 0;
}

View File

@@ -0,0 +1,20 @@
#include <array>
#include <cstdio>
#include <cstdlib>
#include <exception>
int main() {
constexpr auto my_terminate_handler = []() {
printf("This is my_terminate_handler\r\n");
std::abort();
};
std::set_terminate(my_terminate_handler);
std::array<int, 4> arr;
for (int i = 0; i < 5; i++) {
arr.at(i) = i;
}
return 0;
}

View File

@@ -0,0 +1,30 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <vector>
void *operator new(std::size_t count) {
printf("%s, size = %ld\r\n", __PRETTY_FUNCTION__, count);
return std::malloc(count);
}
void operator delete(void *ptr) noexcept {
printf("%s\r\n", __PRETTY_FUNCTION__);
std::free(ptr);
}
int main() {
std::vector<std::uint8_t> vec;
constexpr std::size_t n_elem = 8;
for (std::uint8_t i = 0; i < n_elem; i++) {
vec.push_back(i);
}
const auto print_array = [](uint8_t *arr, std::size_t n) {
for (std::size_t i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\r\n");
};
print_array(vec.data(), n_elem);
return 0;
}

View File

@@ -0,0 +1,33 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <vector>
void *operator new(std::size_t count) {
printf("%s, size = %ld\r\n", __PRETTY_FUNCTION__, count);
return std::malloc(count);
}
void operator delete(void *ptr) noexcept {
printf("%s\r\n", __PRETTY_FUNCTION__);
std::free(ptr);
}
int main() {
std::vector<std::uint8_t> vec;
constexpr std::size_t n_elem = 8;
vec.reserve(n_elem);
for (std::uint8_t i = 0; i < n_elem; i++) {
vec.push_back(i);
}
const auto print_array = [](uint8_t *arr, std::size_t n) {
for (std::size_t i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\r\n");
};
print_array(vec.data(), n_elem);
return 0;
}

34
Chapter02/vector_pmr.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <array>
#include <cstdio>
#include <cstdlib>
#include <memory_resource>
#include <new>
#include <vector>
void *operator new(std::size_t count, std::align_val_t al) {
printf("%s, size = %ld\r\n", __PRETTY_FUNCTION__, count);
return std::malloc(count);
}
int main() {
using namespace std;
using namespace std::pmr;
constexpr size_t n_elem = 8;
array<uint8_t, sizeof(uint8_t) * 8> buffer{0};
monotonic_buffer_resource mbr{buffer.data(), buffer.size()};
polymorphic_allocator<uint8_t> pa{&mbr};
std::pmr::vector<uint8_t> vec{pa};
// vec.reserve(n_elem);
//
for (uint8_t i = 0; i < n_elem; i++) {
vec.push_back(i);
}
for (uint8_t data : buffer) {
printf("%d ", data);
}
printf("\r\n");
return 0;
}