I was bored and wanted to make a program to crash my computer :P. I would have it uselessly and redundantly allocate memory until the crash. The code I created so far is here:
#include <cstdlib>
#include <cstdio>
int main(int argc, const char *argv)
{
int n = 0;
while (1)
{
n++;
int* buffer = (int*)malloc(n ^ n);
int* buffer_buffer = (int*)calloc(n, sizeof(buffer));
for (int i = 0; i < n; i++) {
printf("%p", &buffer);
printf("\n");
buffer_buffer[i] = (int)buffer;
}
}
}
The code works(it crashes the computer), but does not work as expected.I wanted to go more into the process of the how it worked and what exactly it was doing, so I set a few breakpoints and decided to step through it. I expected to see the buffer_buffer
reallocated again and again containing n
numbers of buffer
, but it does not. Instead, my debugger shows that buffer_buffer
contains a single value that sometimes will change, and a single value(the integer cast of buffer
at least I hope) is logged every go round the loop. I was expecting the buffer_buffer
to grow with n
number of elements every time around the for loop comes around, but it only has one element. To visualize this, here is a screenshot of the debugger:
Again I am somewhat tired and this is probably an issue with my loop logic. Does anyone know why my program is experiencing this unexpected behavior? I am using the Microsoft Visual Studio debugger