fork(1) 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 main() {
  9. head_t* h = static_cast<head_t*>(::operator new(sizeof(head_t) + 10 * sizeof(int)));
  10. h->size = 10;
  11. int* my_10_ints = h->data;
  12. // Oh noez! I forgot 10!
  13. size_t what_was_10_again = static_cast<head_t*>(static_cast<void*>(my_10_ints) - offsetof(head_t, data))->size;
  14. ::std::cout << what_was_10_again << "\n";
  15. ::operator delete(static_cast<void*>(my_10_ints) - offsetof(head_t, data));
  16. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
10