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

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

I don't know why, but using std::array causes RTE in official contests.

In a local contest, there was a problem that I solved using matrices, and because I was used to using Array<> to store the values of the matrices, I faced weird run time errors if I compiled my solution using the Test button on the PC2 on my machine, or if I submitted my solution to the judge. Not just in the locals, but also in the recent 2023 ACPC competition there was a similar problem and some teams that didn't know of the problem faced the same issue which caused them to lose most of the contest's time trying to figure things out without any luck.

It's also a good idea to use the Test button on the PC2 before submitting any problem to catch such errors or to check if the problem was solved.

If anyone know why such a behavior is happening or how to use Array<> without getting an RTE, please share the solution.

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

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

Try asking your local committee to follow the ICPC World finals programming environment https://docs.icpc.global/worldfinals-programming-environment/ to prevent this kind of things happening again

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

    They said that they are using the same environment that is going to be used in the next ICPC in Luxur.

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

      According to the linked page, they are not. WF uses DOMJudge.

      Also, who is still using PC^2 in 2024?

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

        That hurts, in the ACPC, they were supposed to test the systems that are going to be used next month.

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

Are you sure you initialized the arrays properly? Something like "std::array<int,2> x;" as a local variable leaves the values uninitialized. I had a bug like that before.

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

    Yes, and I wasn't the only one having the same problem, and even so, that wouldn't give an RTE problem on sample test cases at least.

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

maybe give code first if possible

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

    I don't have the code rn, but what I did then, is that I only replaced std::array, with std::vector, and got an AC.

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

      So maybe std::array causes stack overflow? When it is created as local variable, it is allocated in stack. Did you try using simple C-like array int x[N]; instead of std::array? Did it give same runtime error?

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

        no, I didn't try a C-like array, but I think this problem is related to the compiling flags that are being used because if I compiled and ran the sample test cases using the code blocks environment, everything would be fine. but if I used the Test button on the PC2, it would return RTE.

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

          It's possible that the stack sizes are different in both runs, for example, if PC2 is a different machine (I don't know what you mean by PC2).

          Using a std::vector of std::vector for a matrix is much slower than std::array of std::array, at least for small matrices (you can see this in the matrix exponentiation gym for example), so you might want to do something like auto& dp = *(new std::array<std::array<int, N>, M>()); instead to allocate on the heap.

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

    some of the teams that faced the same problem, used c-like arrays in a template such as:

    struct Matrix {
        int dp[110][110];
        Matrix() {
            memset(dp, 0, sizeof dp);
        }
    };