2

I just started learning C and I want to make some experiments to get used to important elements of the language, like pointers, memory management and so.

I was wondering if I can make a program that wastes 1 GB of your RAM. Like when you execute the program, you can actually see in your Task Manager how magically 1GB of RAM is being used.

The code I think could be used would be:

int * wasteOneGigOfRAM()
{
    int *p = (int *)malloc((sizeof(int) * 256) * 1024 * 1024);
    return p;
}

That function returns the pointer so that memory can be free later with another function, something like

int * waste = wasteOneGigOfRAM();
//Have fun seeing the task manager
// ...
free(waste);

The functions will be called via menu, so I can fill the RAM and then with a menu item I can free it.

But the first function won't fill one gig of RAM. How can I actually achieve it?

trincot
  • 317,000
  • 35
  • 244
  • 286
Juan Bonnett
  • 1,823
  • 1
  • 15
  • 33

0 Answers0