brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · 13402e3 Raw
41 lines · cpp
1#include <cstring>2#include <memory>3#include <string>4 5int main() {6  // Stack7  const char stack_pointer[] = "stack_there_is_only_one_of_me";8 9  // Heap10  // This test relies on std::string objects with size over 22 characters being11  // 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 Heap18  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 boundary22  void *aligned_string_ptr = new size_t[buffer_size];23  if (aligned_string_ptr == nullptr) {24    return -1;25  }26  // Zero out the memory27  memset(aligned_string_ptr, 0, buffer_size);28 29  // Align the pointer to a multiple of 8 bytes30  size_t size = buffer_size;31  aligned_string_ptr = std::align(8, len, aligned_string_ptr, size);32 33  // Copy the string to aligned memory34  memcpy(aligned_string_ptr, aligned_string, len);35 36  (void)stack_pointer;37  (void)heap_pointer1;38  (void)heap_pointer2; // break here39  return 0;40}41