Start with something like
#define SIZE 102400
...
long currentSize = SIZE;
...
char * p;
...
p = (char *) malloc(currentSize)
while (p != null)
{
print message that you can allocate currentSize bytes.
change currentSize to currentSize + SIZE
you are going to reassign p, so free the memory it points to or
you will have a memory leak:
free(p);
try to allocate the new amount
p = (char *)malloc(currentSize)
}
Now you are out of the loop, so malloc must have failed. Print an error
message. Use perror(). perror() is almost alway followed by exit(1);
perror("some message you make up");
Note that this will result in output:
"some message you make up" plus a colon plus a space plus an
appropriate system error message, as in
some message you make up: cannot allocate memory
If you need something fancier, e.g. to include the size on which malloc
failed, you can use strerror(errno), as in
fprintf(stderr, "cannot malloc %ld bytes: %s\n", currentSize, strerror(errno));
Normally you would use just one of these methods of writing an error message,
but for this exercise use both.
Finally quit, but with non-zero exit status
exit(1);