Created
January 28, 2013 11:02
-
-
Save nicdoye/4654638 to your computer and use it in GitHub Desktop.
Allocate RAM wait a minute and exit. Useful for flushing caches etc. Can chose between malloc and calloc.
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
| /* | |
| Program to allocate MB of memory. Can chose between calloc and malloc implementations. | |
| Author: nic doye http://worldofnic.org | |
| Date: 2011.03.31 | |
| Last update: 2012.10.16 | |
| */ | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <errno.h> | |
| extern int errno; | |
| void usage(char *); | |
| int main( int argc, char *argv[]) | |
| { | |
| size_t meg = 1024 * 1024; | |
| size_t elsize = sizeof(char); | |
| char *result; | |
| char *progname = argv[0]; | |
| if ( argc != 3 ) | |
| { | |
| usage(progname); | |
| } | |
| else | |
| { | |
| char *p; | |
| errno = 0; | |
| long number_of_meg = strtol(argv[2], &p, 0); | |
| if ( !strcmp(p,argv[2]) ) | |
| { | |
| printf ( "Command line error (%d) \n", errno ); | |
| } | |
| else | |
| { | |
| size_t nelem = meg * number_of_meg; | |
| printf ("Allocation %zu of size %zu\n", nelem, elsize); | |
| errno = 0; | |
| if ( !strncmp(argv[1],"c",0) ) | |
| { | |
| result = (char *) calloc( nelem, elsize ); | |
| } | |
| else if ( !strncmp(argv[1],"m",0) ) | |
| { | |
| result = (char *) malloc( nelem * elsize ); | |
| } | |
| else | |
| { | |
| usage(progname); | |
| } | |
| if ( result == NULL ) | |
| { | |
| if ( &errno != NULL ) | |
| { | |
| perror ( "Memory error" ); | |
| } | |
| } | |
| else | |
| { | |
| sleep( 60 ); | |
| } | |
| } | |
| } | |
| } | |
| void usage(char *progname) | |
| { | |
| printf("Usage: %s [c|m] n\nwhere c would call calloc and m malloc\nand n is the number of MB to allocate\n", progname); | |
| exit(-1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment