Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <malloc.h>
  4. #include <assert.h>
  5.  
  6. const size_t mem_size = 16L * 1024*1024*1024;
  7. const size_t block_size = 20 * 1024 * 1024;
  8. const size_t blocks_count = (mem_size / block_size) / 2;
  9.  
  10. int main() {
  11. void* p;
  12. /*{
  13. p = malloc(block_size);
  14. free(p);
  15. }*/
  16.  
  17. printf("Allocate 1/2 of the main memory\n");
  18. void** blocks = new void*[blocks_count];
  19. for (size_t i = 0; i < blocks_count; ++i) {
  20. blocks[i] = malloc(block_size);
  21. memset(blocks[i], 0, block_size);
  22. }
  23.  
  24. printf("Free 1/4 of the main memory\n");
  25. for (size_t i = 0; i < blocks_count; i+=2)
  26. free(blocks[i]);
  27.  
  28. printf("Allocate big array (1/2 of the main memory)\n");
  29. p = malloc(mem_size / 2);
  30. printf("Initialize big array\n");
  31. memset(p, 0, mem_size / 2);
  32. printf("OK\n");
  33.  
  34. free(p);
  35. delete [] blocks;
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement