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