Why do online compilers e.g Programiz sometimes get different outputs from code editors like Visual Studio Code? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Why do online compilers e.g Programiz sometimes get different outputs from code editors like Visual Studio Code?

I was trying to compile this and I was shocked. Online gdb/Programiz/Paiza.io's outputs were all hlpmme, not hlp me. This app and Visual Studio ran the code well, so I really do not understand what happened. char s[300]="help me"; strcpy(s+1, s+2); cout<<s; I also had a similar issue a long time ago when I was working with floats.

19th Sep 2022, 6:01 PM
Denis
Denis - avatar
6 Antworten
+ 5
Brian There is no "correct" output in this scenario, since calling strcpy() with overlapping strings is undefined behaviour. Also, s + 1 is not the same as s[1], since the resulting pointer is not dereferenced.
19th Sep 2022, 9:30 PM
Shadow
Shadow - avatar
+ 4
Thanks Ipang. Shadow thanks for your reply. Now I understand that not all implementations use safe memory moves that properly handle overlapped memory regions. I think you happened to see my reply about s[1] before I edited it to add the pointer reference. Nonetheless, thank you for pointing that out, too. I had begun typing up an explanation, but I think the Stack Overflow link covers it better.
20th Sep 2022, 4:20 AM
Brian
Brian - avatar
+ 2
What does adding an integer to a char array mean in C++?
19th Sep 2022, 6:29 PM
Lisa
Lisa - avatar
+ 2
Denis as you expected, the correct output from any compiler and string library should be "hlp me". It seems that Programiz has a bizarre version of strcpy that skips past whitespace. I have never seen that before. It is perplexing to imagine why that would be useful. Lisa s+1 is doing pointer math. It is the same as &(s[1]).
19th Sep 2022, 9:07 PM
Brian
Brian - avatar
+ 2
Its not cause of the compiler... U might even get different result on same compliler in thi situation
21st Sep 2022, 7:45 AM
Michael Mamo
Michael Mamo - avatar