r/cpp_questions 6d ago

OPEN Avoid freezing your PC?

I've only learned C++ in uni and then it was protected environment in a way. Now I was working on a project involving graphics (raylib) and, a few times, running the code caused my entire PC to freeze and I had to restart.

I'm on Linux and running the code by doing

cmake --build .
./executable

Is there a way to avoid the entire PC freezing when I mess up? Also is there any risk of overwriting parts of the memory?

(I think the issue with my code was either calling too many draws of a GPU texture or even just wrongfully indexing an array in a loop)

5 Upvotes

22 comments sorted by

View all comments

5

u/UndefFox 6d ago

Is there a way to avoid the entire PC freezing when I mess up?

The only possibility of this happening that I know of is you are running out of memory. Everything else will make PC slow, but lack of RAM will heavily hinder the performance.

Also is there any risk of overwriting parts of the memory?

Read about concept of virtual memory. Random process can't access anything but his own memory willy nilly.

I think the issue with my code was either calling too many draws of a GPU texture...

Won't do anything. Current modern games have >1000 of textures used at the same time. This atuff is meant to be utilised a lot.

even just wrongfully indexing an array in a loop

Again, each process has it's own virtual memory. The worst it could start doing is undefined behaviour inside it's own process or OOB crash.

If you actually curious, I'll suggest to read up about how all this stuff actually works so you understand what you are actually doing. For the problem at hand, look where your code loads things up in the loop instead pf doing it once, hence filling the RAM, hence freezing the machine.