azymohliad

Memory overcommit example

Jan 10th, 2020
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define ARRAY_SIZE 1024
  5. #define ELEMENT_SIZE 1<<30 // 1GB
  6.  
  7. int main() {
  8.     void* ptr[ARRAY_SIZE];
  9.     for (int i = 0; i < ARRAY_SIZE; i++) {
  10.         ptr[i] = malloc(ELEMENT_SIZE);
  11.         if (ptr[i] == NULL) {
  12.             printf("Allocation failed\n");
  13.             break;
  14.         }
  15.     }
  16.     printf("Allocation finished\n");
  17.     getc(stdin);
  18.     return 0;
  19. }
RAW Paste Data