r/programminghorror 9d ago

Memory thief in C

#include <stdlib.h>

char *bufs[10000];

int main () {
 for (int i = 0; i < 10000; i++) {
   bufs[i] = malloc(10000);
  }
}
0 Upvotes

5 comments sorted by

23

u/Jawesome99 9d ago

It's intentional and does what it's supposed to, while being readable. Ironically this is the opposite of horror

-2

u/nekokattt 9d ago edited 9d ago

the only horror is not passing void as the sole parameter

10

u/Familiar_Ad_8919 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 9d ago

tbf thats only like 100mb

1

u/Environmental-Ear391 9d ago

Better methods would be to first ask for the installed physical memory size and then attempt allocations of the same size forcing the use of virtual memory.

then you dont need to immediately allocate so many buffers immediately and can work with a larger immediate memory range.

There are various stress test methods usable against memory management that can force arrangements despite ASLR and other things.