On modern typical desktop and server operating systems, such a program is simply not possible. The operating system will limit each process' use of resources, and might kill it if it goes way overboard.
The C equivalent would be something like:
#include <stdlib.h>
int main(void)
{
const size_t chunk = 1 << 20;
for (;;)
{
void *p = malloc(chunk);
if (p != NULL)
memset(p, '?', chunk);
}
return 0;
}
The above allocates 1 MB for each iteration of the loop. The memset()
is an attempt to actually use the memory by setting all bytes to something non-zero; without that the operating system might be lazy-allocating the memory and not actually dedicating physical RAM until needed.
I tried this code on ideone.com, and it gets killed after 750 ms and about 2 GB of memory spent. Not providing a link to the actual code, to be kind to ideone.
Update: now, if you really wanted to do this for some reason (like RAM testing), then your execution environment cannot be "as a process in a general-purpose operating system" which tends to be the default assumed by at least me for answering questions. If you're running on the bare metal without an OS, say from the boot block or in a OS loader or something, then the above won't work either.
In that case, you would probably (I've never done this on PC-class hardware) need to query BIOS for information about where the RAM is in the address space, then create pointers to those areas and use a manual loop or memset()
if you link with enough of the standard library to have it. That is a rather more specialized question, though.