NoPaste

Speicher füllen

von king-crash
SNIPPET_DESC:
Reserviert schrittweise Arbeitsspeicher.
SNIPPET_CREATION_TIME:
30.09.2022 09:03:35
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <sys/wait.h>
  7.  
  8. #define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))
  9.  
  10.  
  11. static int size_max_in_100mb = 300; // Maximal 30GB
  12. static int use_memory = 1;                      // Bei 0 wird der Speicher nur reserviert aber nicht tatsächlich genutzt.
  13.  
  14. int main(void)
  15.         {
  16.         void *buffer[size_max_in_100mb];
  17.         memset(buffer, 0, sizeof(buffer));
  18.         size_t size = 100000000;
  19.         int count;
  20.         for(count=0;count<NELEMS(buffer);count++)
  21.                 {
  22.                 usleep(100000);
  23.                 if((buffer[count] = malloc(size)) != NULL)
  24.                         {
  25.                         printf("%dMB\n", (int)(size *(size_t)(count+1) / (size_t)1000000));
  26.                         if(use_memory)
  27.                                 {
  28.                                 memset(buffer[count], 0, size);
  29.                                 }
  30.                         }
  31.                 else
  32.                         {
  33.                         puts("malloc meldet Fehler.");
  34.                         break;
  35.                         }
  36.                 }
  37.         sleep(3);
  38.         if(buffer[count] == NULL)
  39.                 {
  40.                 count--;
  41.                 }
  42.         for(;count>=0;count--)
  43.                 {
  44.                 free(buffer[count]);
  45.                 }
  46.         return 0;
  47.         }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN