fork download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <cstddef>
  4. using std::size_t;
  5.  
  6. struct head_t { size_t size; int data[]; };
  7.  
  8. int* make_intarray(size_t const size) {
  9. head_t* h = static_cast<head_t*>(::operator new(sizeof(head_t) + size * sizeof(int)));
  10. h->size = size;
  11. return h->data;
  12. }
  13.  
  14. void unmake_intarray(int* array) {
  15. ::operator delete(static_cast<void*>(array) - offsetof(head_t, data));
  16. }
  17.  
  18. size_t get_intarray_size(int const*const begin) {
  19. return static_cast<head_t const*>(static_cast<void const*>(begin) - offsetof(head_t, data))->size;
  20. }
  21.  
  22. int main() {
  23. int* my_10_ints = make_intarray(10);
  24. // Oh noez! I forgot 10!
  25. size_t what_was_10_again = get_intarray_size(my_10_ints);
  26. ::std::cout << what_was_10_again << "\n";
  27. unmake_intarray(my_10_ints);
  28. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
10