Ziad0_0's blog

By Ziad0_0, history, 7 years ago, In English

Hi, I am trying to solve 118A - Упражнение на строки and the output I have in my pc is different than the output in codeforces. My submission:26568938

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

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

Why I am downvoted like that?

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

declare str1 and str2 as global variable ;) 26571652

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

    Thank you very much ^_^ , I really appreciate your help, but if you know can you please tell me why the output has changed ? or what was the issue ? Thank you again :)

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

      I think unintitialized string is undefined behavior (UB) so anything could happen. Different compliers (even in same compliers) may generate different outputs. In order to avoid this problem, You can initialize the array elements to 0 by declare as global or static variables.

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

        Just give str2 an initializer like char str2[101] = {0}; is OK.

        But in competitive programming it's recommended to declare large arrays static/global to avoid stack overflow. This array is small enough and Codeforces is using -Wl,--stack=xxxxxxxxxx so this won't happen.