jweinst1
6/25/2017 - 1:40 AM

example using allocator

example using allocator

// Example program of allocation
#include <iostream>
#include <memory>

struct foo {
    void* data;
};

int main()
{
  std::allocator<foo> items;
  foo* fptr = items.allocate(1);
  foo* aptr = fptr;
  fptr->data = new int(2);
  int* h = (int*)fptr->data;
  
  std::cout << *h << std::endl;
  //2
  items.deallocate(fptr, 1);
  h = (int*)aptr->data;
  std::cout << *h << std::endl;
  //no value, data has been deleted by allocator
}