Created
April 24, 2017 07:32
-
-
Save oscarsaleta/11c66f5d71752406f61d93120a4ccdd1 to your computer and use it in GitHub Desktop.
Allocates a given amount of MB of memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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