#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define DSIZE 16000	/* Default size of each memory allocated */

int main(int argc, char *argv[])
{
	unsigned int i;
	int size = DSIZE;
	float total = 0;
	
	if(argc > 1){
		for(i = 0; i < strlen(argv[1]); i++){
			if(!isdigit(argv[1][i])){
				fprintf(stderr, "Argument is not a digit. (Stopped at character %d.) Exiting...\n", i);
				exit(-1);
			}
		}
		size = atoi(argv[1]);
	}

	printf("Putting packets of %d bytes into memory...\n", size);

	for(; malloc(size) != NULL; total += (float)(size)/(float)(1024))
		printf("Total: %f KB\n", total);
		;

	printf("Err: Could not allocate more memory!\n");
	printf("So long, I have managed to allocate %f KB of memory...", total);

	return 0;
}
