-3

I need a program, that will fill my whole memory.

Preferably in C, a tiny program,

Suggestions?

I have tried the following program in c++, but I need it in C:

#include<bits/stdc++.h>
  
int main()
{
    while (true)
      int *a = new int;  // allocating 
}
  • 2
    Define 'fill my whole memory'. –  Nov 07 '22 at 08:42
  • 2
    You allocate in C with `malloc`. – Paul Hankin Nov 07 '22 at 08:43
  • 2
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Whomever taught you about that header file should be taken out back and have a stern talking to. – Some programmer dude Nov 07 '22 at 08:46
  • c program that saturates the whole ram memory. I don't know if I'm making myself clear, my level of English is a bit low. – dhhdjdj bfjdjdfjfj Nov 07 '22 at 08:46
  • So you want to allocate memory until it fails? Why? What is your original problem? – Some programmer dude Nov 07 '22 at 08:48
  • The whole RAM memory of what? The process? Your microcontroller? Some specific PC? – Lundin Nov 07 '22 at 08:48
  • Note that the operating system is likely to support virtual addressing, which means your program might be hitting swap space. Since it's not using the memory it allocated, the page scheduler will probably just end up swapping stuff to disk. Your process will probably not starve other processes of RAM, if that's what you're asking. Not on a 64-bit system, anyway. – paddy Nov 07 '22 at 08:52
  • On Linux memory allocations doesn't actually happen when calling `malloc`. The system won't map actual memory to a process until the memory is actually used in some way. This can lead to *over commit* where applications allocate memory that the system can't provide. – Some programmer dude Nov 07 '22 at 08:56

1 Answers1

0

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • @Lundin That would be sweet, but perhaps this quality contribution is better with fewer `const`. Thanks. – unwind Nov 07 '22 at 08:49
  • To investigate some PC problems on Windows 10, I did something similarly. Instead of constant bytes the program filled the allocated space with random bytes. Anyway, it was not killed by the OS. On the contrary, it lead to a frozen system as soon as all physical RAM and the swap was filled. Not even a BSOD. – the busybee Nov 07 '22 at 09:17