Advertisement
Guest User

omfgroflyolofrilug

a guest
Dec 28th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <new>
  2.  
  3. constexpr unsigned long limit = 10000000; // 10MB
  4. char *memory[limit];
  5. unsigned long in_use_count = 0;
  6.  
  7. void *alloc(unsigned long size)
  8. {
  9.     if (in_use_count + size >= limit)
  10.         throw std::bad_alloc();
  11.  
  12.     unsigned long ptr = in_use_count;
  13.     in_use_count += size;
  14.     return (void *)&memory[ptr];
  15. }
  16.  
  17. void *operator new(unsigned long size)
  18. {
  19.     return alloc(size);
  20. }
  21.  
  22. void *operator new[](unsigned long size)
  23. {
  24.     return alloc(size);
  25. }
  26.  
  27. int main()
  28. {
  29.     int *my_ints = new int[20];
  30.     for (int i = 0; i < 20; i++) {
  31.         my_ints[i] = i;
  32.     }
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement