Code: Select all
#include <stdlib.h>
#include <stdio.h>
void fail(char *s)
{
puts(s);
putchar(7); /* beep */
exit(1);
}
main(int argc, char *argv[])
{
char *p;
unsigned long int kb;
unsigned long int allocs;
if (argc < 2)
kb = 1024; /* default 1 megabyte, equivalent to 1024 kilobytes */
else
kb = strtoul(argv[1], &p, 10); /* command-line option: n kilobytes */
printf("\nAllocating %lu kilobyte%s... ",
kb, ((kb == 0) || (kb > 1)) ? "s" : "");
for (allocs = 0; allocs < kb; allocs++)
if ((p = malloc(1024)) != 0) /* in 1 kilobyte blocks */
{
*p = 'x'; /* do something, anything with */
p[1023] = 'y'; /* the allocated memory */
}
else
{
printf("only %lu kilobyte%s available\n",
allocs, ((allocs == 0) || (allocs > 1)) ? "s" : "");
fail("Insufficient memory!");
}
puts("ok");
return 0;
}
It is possible to do a version in FreeBasic? Don´t know if FreeBasic has a function equivalent to malloc in C