mem_eater

[Done] A program that will eat up all your ram memory. Keep it safe...
git clone https://git.jojolepro.com/mem_eater.git
Log | Files | Refs

commit c80d00e7e20db7c75b5e1eb55b9fc44b214144e8
Author: Joël Lupien (Jojolepro) <jojolepro@jojolepro.com>
Date:   Tue, 25 Aug 2020 15:50:47 -0400

init

Diffstat:
Amain.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+), 0 deletions(-)

diff --git a/main.c b/main.c @@ -0,0 +1,50 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +void* allocate(void* mem, size_t size, size_t oldsize) { + if (!mem) { + mem = malloc(size); + + if (!mem) { + fprintf(stderr, "malloc failed!\n"); + return NULL; + } + } else { + void* ptr = realloc(mem, size); + if (!ptr) { + fprintf(stderr, "realloc failed!\n"); + return mem; + } else { + mem = ptr; + } + } + + // fill from `oldsize` to end + memset(mem + oldsize, 0, size - oldsize); + + return mem; +} + +int main(void) { + void* mem = NULL; + + const size_t initial_block = (size_t)(1024 * 1024 * 1024) * (size_t)28; + size_t size = initial_block; + // allocate 28 GB + mem = allocate(mem, size, 0); + + char c; + while ((c = getchar()) != 'q') { + // allocate 512 MB + const size_t block = 1024 * 1024 * 512; + size += block; + mem = allocate(mem, size, size - block); + } + + if (mem) { + free(mem); + } + printf("memory freed\n"); + return 0; +}