riela's blog

By riela, history, 5 years ago, In English

Some weeks ago, I was coding a solution in c++, and by mistake I was adding an infinite amount of numbers to a vector. Suddenly, my computer got frozen, and it didn't respond to keyboard neither mouse events. I had to reset my computer, and it took a lot of time fixing the disk.

Did you had similar experiences during important contests?

Is there a compìler configuration to prevent this kind of scenarios?

  • Vote: I like it
  • +51
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it -24 Vote: I do not like it

Sometimes my laptop crashes when I open up facebook. Never had any issue with programs though. I'm pretty sure the program's resources are limited by the system to prevent things like this from happening.

Are you using linux by any chance?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    yes, I'm on linux, I compile and execute from the terminal

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it -103 Vote: I do not like it

      I have only ever had bad experiences with linux, maybe you should switch to proprietary operating systems.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +16 Vote: I do not like it

        that is not a solution! ioi and icpc use both linux, so it is a good idea to get familiar with the environment

      • »
        »
        »
        »
        5 years ago, # ^ |
        Rev. 2   Vote: I like it +55 Vote: I do not like it

        I have only ever had good experiences with Linux. Maybe you should consider buying the bridges I'm selling.

        (Also, when your laptop now crashes because you open a site, I don't want to know what are "bad experiences" compared to that.)

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it -43 Vote: I do not like it

          I had a lot of pretty nasty experiences trying to configure ubuntu (which is apparently supposed to be user-friendly).

          My laptop is 4 years old macbook pro and it craps out a fair bit (the facebook thing happened just now). I don't like it at all either. That's why I don't do important work on it.

          My desktop is 2 years old on windows and it has only crashed a couple times so far. Everything just works out the box.

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it +33 Vote: I do not like it

            it has only crashed a couple times so far

            Is this really the standard we should be setting for proprietary OSes...

            • »
              »
              »
              »
              »
              »
              »
              5 years ago, # ^ |
              Rev. 5   Vote: I like it -26 Vote: I do not like it

              it is better than having no standard at all for personal-use linux (of course, I'm not talking about general server/business usage where there are support teams and companies maintaining distributions and such)

              Plus, a crash in every couple of months to half-year isn't unbearably bad

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it -12 Vote: I do not like it

            Exactly nothing is easy to install. Nothing actually works on Ubuntu. Videos freeze time after time. Now I can't hear from the left earpiece (while on win10 it is working) out of nowhere.

            • »
              »
              »
              »
              »
              »
              »
              5 years ago, # ^ |
              Rev. 2   Vote: I like it -14 Vote: I do not like it

              Yeah, I tried to use ubuntu again recently and got hit by an avalanche of problems, chiefly the mouse losing focus and not clicking stuff behind a "stuck" application. I found the perfect stackoverflow answer, except it was for a different mouse brand and it didn't help at all...

              I find it interesting that there are so many proprietary haters here when in linux apparently your program can force you to reset your whole computer (how is that even remotely acceptable?!!?!??!?!), and the whole thing needs a force shutdown from an infinite loop. But apparently a crash every few months is a "bad standard". In my mind, it's better than losing your whole disk...

              I don't see why people hate the thought of proprietary os so much. Ok, some people can use linux successfully, grind through each error, and enjoy using it, but that doesn't mean switching is necessarily a bad idea.

»
5 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Something similar happened to me once. One way to get around it is to limit memory per process and set timeout

»
5 years ago, # |
  Vote: I like it +23 Vote: I do not like it

ulimit -a will show you various limits. If you have unlimited stack size limit, I recommend changing it to e.g. 256MB (ulimit -s 256000). There are also some other limits for memory, but I never used/changed them.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Many times due to infinite loop entire ubuntu stops responding. Then force shutdown, using power button is the only option.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    Seems very strange. Sounds like infinite recursion and thus growing memory consumption. An infinite loop shouldn't take much resources, I think.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +22 Vote: I do not like it

    Tip: Often it is enough just to wait few minutes until the OOM rips your process.

    • »
      »
      »
      5 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Does this work on debian based distros like ubuntu? I have seen much discussion on OOM killer not invoking on Ubuntu 14 and above.

      PS: I decided to give it a try and I am still waiting for OOM killer to kick in while writing this comment using mobile. Hope it gets invoked.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Sublime C++14 Build

    This Sublime build will stop executing the program if it's execution time goes more than 0.5s, thus saving you the trouble of restarting your machine. You can set the timeout to a higher value. 0.5s works fine in my laptop.

»
5 years ago, # |
  Vote: I like it +22 Vote: I do not like it

As I said much much earlier, advanced technology so often go wrong. You can never go wrong with notepad. Replace .txt with .cpp and submit. Now you might wonder: "what if i make bug?" Well tourist once said, best way to debug is to not make bug in first place. Notepad train your mind, and your coding skill, no downsides.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    What about IOI? It uses linux so I should get used to it.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      Well, if you use notepad you can adapt to all environments ;)

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Um, why are you talking to yourself?

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Well although I address his concerns, I also realize I did not answer the question about how this is useful for linux. So I decided to ask the poster (me). Of course, since I asked myself I also have to answer myself.

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Ah, makes perfect sense, carry on.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +7 Vote: I do not like it

    ... and brdy says that real programmers use notepad lol

    is good to get used to linux, maybe you can't even open notepad (or maybe you can realize that there is no notepad at all!)

    also it seems you don't undersand the problem, even if you code in "notepad",after compiling and executing it can crash your computer because a bug, and yes even tourist is human and write some bugs.

    IDEs are also a good thing (at least for java). I think petr could decrease his performance if he codes in notepad

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +12 Vote: I do not like it

      Sorry if I was not clear enough, as the technique I mentioned is very advance.

      The basic idea is that you cannot crash your computer if you never run your code in the first place. So that is why I say we use notepad, and then convert it to a cpp file submit it. Of course, we do not need to debug if do not make bugs in the first place, and thus we never need to run our code locally.

      If any other questions & concerns I will be in touch. Have nice day.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +64 Vote: I do not like it

        very advanced stuff, maybe I'll try that after reaching blue

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it -18 Vote: I do not like it

          Why not use it now? It solves your problem, now your computer will not crash.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it -7 Vote: I do not like it

        But why to use .txt at first place then? Use some editor with .cpp extension only and then directly submit that without running it.

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it -8 Vote: I do not like it

          You sir, are genius. I congratulate you. But for some users this optimization is too advanced, to we start with the basics (.txt files)

»
5 years ago, # |
  Vote: I like it +18 Vote: I do not like it

But have you ever made a bug that crashed your OS? The one you described just caused slow or exhausted swap (maybe it was zero-size?). The OS didn’t crash until you hard reset it.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I didn't have a bug, but I was testing a function for a linked list for 2 billion nodes. Apparently, 2 billion nodes was too much (but I didn't know that!), and I had the same thing happen to me. It froze after about 10 million, and bad things happened. I'd be curious to see if there's a way to prevent these scenarios.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    I'd be curious to see if there's a way to prevent these scenarios.

    Get a supercomputer?

»
5 years ago, # |
Rev. 2   Vote: I like it +14 Vote: I do not like it

you can use setrlimit syscall on linux.

#include <vector>                                                                                     
#include <sys/resource.h>                                                                             

int main() {                                                                                          
  rlimit cpu_time{.rlim_cur = 1, .rlim_max = 2};                                                      
  setrlimit(RLIMIT_CPU, &cpu_time);                                                                   

  std::vector<int> v;                                                                                      
  while (true) v.push_back(1);                                                                        
}

this program would not be allowed to consume more than 2 seconds (rlim_max) of CPU time. You can set other limits too. Reference: http://man7.org/linux/man-pages/man2/setrlimit.2.html

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This happened, because of the way C++ std::vector works internally. It allocates an array of objects of required size dynamically (on heap). When the array becomes full (using push_back ), it allocates one with double the original size, and copies all the elements. Since heap size is bounded only by the size of RAM (or virtual memory) on your PC, it kept allocating infinite memory leading to lack of space of all other processes, and crash. So, a solution to this would be 1. Not having a vector (or any container) of objects of size more than 10^7. 2. Keeping time and memory limits on your code like others have suggested. 3. Keeping your memory usage in check.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I have a problem during the last subregional of ICPC South America. 15 minutes left to finish the contest a bug caused the computer to got frozen. It took a good time to reset the computer.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It happened to me 2 years ago in an offline contest.

I wrote a code that generate Fibonacci strings (i.e. s1 = "a", s2 = "b", sn = sn - 2 + sn - 1, the limit of n was such high that the string might have about 1018 characters, if enough memories could be provided).

And when I ran it, the computer I used froze immediately, forcing me to force-restart it :D

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Why doesn't the OS provide anything to control the amount of memory used by a process?

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I'm not even sure that it was my fault or a notorious coincidence, but yup, it did happen.

      Tested it again several times with my personal laptop, and none of the time it froze like expected (RAM usage always at approx. 1GB)