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

int main(int argc, char **argv)
{
	if (argc != 2) {
		printf("Usage: overcommit [GB]\n");
		return 0;
	}

	size_t n = 1024 * 1024 * 1024 * strtod(argv[1], NULL);

	printf("Allocating %zu bytes.\n", n);

	char *ptr = malloc(n);

	if (ptr == NULL) {
		printf("Allocation failed\n");
		return 0;
	}

	printf("Allocation succeeded, got pointer %p\n", ptr);

	for (size_t v = 0; v < n; v++) {
		if ((v % (1024 * 1024  * 1024)) == 0)
			printf("Wrote %zd GB\n", v >> 30);

		ptr[v] = v;
	}

	free(ptr);
	return 0;
}
