1 | #include <cstring> |
---|---|
2 | #include <memory> |
3 | #include <string> |
4 | |
5 | int main() { |
6 | // Stack |
7 | const char stack_pointer[] = "stack_there_is_only_one_of_me"; |
8 | |
9 | // Heap |
10 | // This test relies on std::string objects with size over 22 characters being |
11 | // allocated on the heap. |
12 | const std::string heap_string1("heap_there_is_exactly_two_of_me"); |
13 | const std::string heap_string2("heap_there_is_exactly_two_of_me"); |
14 | const char *heap_pointer1 = heap_string1.data(); |
15 | const char *heap_pointer2 = heap_string2.data(); |
16 | |
17 | // Aligned Heap |
18 | constexpr char aligned_string[] = "i_am_unaligned_string_on_the_heap"; |
19 | constexpr size_t buffer_size = 100; |
20 | constexpr size_t len = sizeof(aligned_string) + 1; |
21 | // Allocate memory aligned to 8-byte boundary |
22 | void *aligned_string_ptr = new size_t[buffer_size]; |
23 | if (aligned_string_ptr == nullptr) { |
24 | return -1; |
25 | } |
26 | // Zero out the memory |
27 | memset(s: aligned_string_ptr, c: 0, n: buffer_size); |
28 | |
29 | // Align the pointer to a multiple of 8 bytes |
30 | size_t size = buffer_size; |
31 | aligned_string_ptr = std::align(align: 8, size: len, ptr&: aligned_string_ptr, space&: size); |
32 | |
33 | // Copy the string to aligned memory |
34 | memcpy(dest: aligned_string_ptr, src: aligned_string, n: len); |
35 | |
36 | (void)stack_pointer; |
37 | (void)heap_pointer1; |
38 | (void)heap_pointer2; // break here |
39 | return 0; |
40 | } |
41 |