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

unsigned int adjustSize(size_t size, char *unitchar)
{
	const char *units = "\0kMGTPEZY"; //Never too early to prepare!
	int loopCount = 0;
	while (size >= 1024 && loopCount < 8)
	{
		size /= 1024;
		loopCount ++;
	}
	if (unitchar) *unitchar = units[loopCount];
	return size;
}

int main(int argc, char *argv[])
{
	size_t size = 1;
	void *buf;

	do
	{
		char unitchar;
		unsigned int sizeadj = adjustSize(size, &unitchar);
		printf("Allocating %u bytes (%u %cB)...", size, sizeadj, unitchar);
		buf = malloc(size);
		if (buf)
		{
			printf("OK\n");
			free(buf);
			size *= 2;
		}
		else printf("Error!\n");
	}
		while (buf);

	return 0;
}
