MemFill

Makefile

CC := gcc
CFLAGS += -Wall

all:                                                                                                                          
        $(CC) $(CFLAGS) -o memfill memfill.c 

memfill.c

#include <stdlib.h>                                                                                                           
#include <stdio.h>                                                                                                            
                                                                                                                         
int main(int argc, char *argv[]) {
        size_t size;
        size_t i;
        char *mem;
        char buf[8];

        if (argc != 2) {
                fprintf(stderr, "USAGE: %s SIZE in MB\n", argv[0]);
                return EXIT_FAILURE;
        }

        size = atol(argv[1]);
        size *= 1024 * 1024;
        if (size <= 0) {
                fprintf(stderr, "ERROR: invalid argument\n");
                return EXIT_FAILURE;
        }

        mem = (char *)malloc(size);
        if (!mem) {
                fprintf(stderr, "ERROR: unalbe to get memory\n");
                return EXIT_FAILURE;
        }

        printf("\nINFO: %ld MB has been allocated\n", size / 1024 / 1024);
        for (i = 0; i < size; ++i) {
                mem[i] = rand() % 256;
                if (i % (1024 * 1024 * 1024) == 0 && i) {
                        printf("\n%ld MB", i / 1024 / 1024);
                        fflush(stdout);
                }
                if (i % (1024 * 1024) == 0) {
                        putc('.', stdout);
                        fflush(stdout);
                }
        }

        puts("\nPress ENTER to continue!");

        fgets(buf, 8, stdin);

        free(mem);

        return EXIT_SUCCESS;
}