Блог пользователя riela

Автор riela, история, 8 лет назад, По-английски

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?

  • Проголосовать: нравится
  • +51
  • Проголосовать: не нравится

»
8 лет назад, скрыть # |
 
Проголосовать: нравится -24 Проголосовать: не нравится

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?

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

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

    • »
      »
      »
      8 лет назад, скрыть # ^ |
       
      Проголосовать: нравится -103 Проголосовать: не нравится

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

      • »
        »
        »
        »
        8 лет назад, скрыть # ^ |
         
        Проголосовать: нравится +16 Проголосовать: не нравится

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

      • »
        »
        »
        »
        8 лет назад, скрыть # ^ |
        Rev. 2  
        Проголосовать: нравится +55 Проголосовать: не нравится

        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.)

        • »
          »
          »
          »
          »
          8 лет назад, скрыть # ^ |
           
          Проголосовать: нравится -43 Проголосовать: не нравится

          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.

          • »
            »
            »
            »
            »
            »
            8 лет назад, скрыть # ^ |
             
            Проголосовать: нравится +33 Проголосовать: не нравится

            it has only crashed a couple times so far

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

            • »
              »
              »
              »
              »
              »
              »
              8 лет назад, скрыть # ^ |
              Rev. 5  
              Проголосовать: нравится -26 Проголосовать: не нравится

              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

          • »
            »
            »
            »
            »
            »
            8 лет назад, скрыть # ^ |
             
            Проголосовать: нравится -12 Проголосовать: не нравится

            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.

            • »
              »
              »
              »
              »
              »
              »
              8 лет назад, скрыть # ^ |
              Rev. 2  
              Проголосовать: нравится -14 Проголосовать: не нравится

              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.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

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

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +23 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +22 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +18 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +14 Проголосовать: не нравится

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

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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