Skip to content

Instantly share code, notes, and snippets.

@oscarsaleta
Created April 24, 2017 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarsaleta/11c66f5d71752406f61d93120a4ccdd1 to your computer and use it in GitHub Desktop.
Save oscarsaleta/11c66f5d71752406f61d93120a4ccdd1 to your computer and use it in GitHub Desktop.
Allocates a given amount of MB of memory
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[]) {
int memsize;
char **mem;
int i;
if (argc != 2 || sscanf(argv[1],"%d",&memsize)!=1) {
fprintf(stderr, "%s: memsize (MB)\n", argv[0]);
return 1;
}
mem = malloc(memsize*sizeof(char*));
for (i=0; i<memsize; i++) {
mem[i] = malloc(1024*1024);
if (mem[i]!=NULL)
memset(mem[i],0,1024*1024);
}
fprintf(stderr,"Press any key to terminate.\n");
fgetc(stdin);
for (i=0; i<memsize; i++)
free(mem[i]);
free(mem);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment