2017ceb1029's blog

By 2017ceb1029, history, 6 weeks ago, In English,

The problem link is : B level And it passes 20 tests but fails on 21st. In an online compiler "CODE" works fine, for the same test case, by failure I mean RUNTIME_ERROR. Any help would be appreciated. The same code first failed at test case 26th, which i noticed was due to less allocated memory when I tripled the allocated space, it fails at 21st case which gives correct output in the other compiler.

Thanks

 
 
 
 
  • Vote: I like it
  • -5
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Quite strange, I reduced the memory allocated by 1.5 and it got accepted. Any explanations why more memory caused failure ?

»
6 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it
char* c= (char* ) malloc(sizeof(char)*len);
scanf("%s",c);

Pretty sure scanf is based on null terminated strings so you need to allocate 1 more byte.

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

    NO, i don't think the problem lies there. I had bounded the use of string by len and not using any /0 terminations. The accepted solution uses the same, only change being the next allocation.

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

      Just so you know, it isn't up to you whether or not a null character is placed there. Currently scanf will always write outside of allocated memory. Maybe your runtime error was caused by something else, but that doesn't change the fact that something is being written outside of allocated memory.

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

      I just tried changing

      char* c= (char* ) malloc(sizeof(char)*len);
      

      into

      char* c= (char* ) malloc(sizeof(char)*(len + 1));
      

      and now it gets AC.

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

        Thanx, probably I was wrong. Anyways i will be careful to use 1 more bit afterwards.