2

When printing a char array directly I get garbage data printed out with it.

#include <iostream>
using namespace std;
int main(){
    char x [5] {'H','E','L','L','O'};
    cout << "Output : ";
    cout << x << endl;
    return 0; }

Output : HELLO@ ~

But, this doesn't happen when I flush the output buffer before printing the char array.

#include <iostream>
using namespace std;
int main(){
    char x [5] {'H','E','L','L','O'};
    cout << "Output : " << flush;
    cout << x << endl;
    return 0; }

Output : HELLO

However, I don't see this behaviour if I don't print anything before flushing output buffer.

#include <iostream>
using namespace std;
int main(){
    char x [5] {'H','E','L','L','O'};
    cout << flush;
    cout << x << endl;
    return 0; }

Output : HELLO}~

The char array in all of the above examples doesn't have the '\0' escape sequence character in the end so when printing the array, stuff inside the memory after the array also gets printed. Why flushing the output buffer showing a different behaviour?

Also, since the 3rd example is printing garbage data at the end inspite of flusing the output buffer. How is the 3rd example different from 2nd?

RNAY
  • 21
  • 4
  • This is undefined behavior, since character string literals, when used like this, must be `\0` terminated. Your compiler is free to produce a program that has any random result. Welcome to C++! – Sam Varshavchik Sep 07 '22 at 10:58
  • thats why I dislike the term "garbage". ppl expect to get soemthing that can be recognized as garbage, when in fact it is just undefined. Something that lookscompletely fine can be as "gargabe" as `HELLO@ ~`, you cannot reliably see the difference – 463035818_is_not_an_ai Sep 07 '22 at 11:01
  • @SamVarshavchik So, these undefined behaviours are compiler specific? If the program is not following the standard convention the compiler is free to produce any result? Also, I am a beginner, is there any online resource where I can learn about all these conventions in C++? – RNAY Sep 07 '22 at 11:16
  • The resulting program produced by the compiler, will have unpredictable results. The best and only resource to learn everything that needs to be learned about C++ [is a good C++ textbook](https://stackoverflow.com/questions/388242/). Any clown can upload a video to Youtube, or publish their rambling streams of consciousness on a web site. Even I can do that. Many have tried to learn C++ by watching videos, reading web sites, or solving dumb coding puzzles. Everyone has failed. It takes money to publish a textbook, noone will risk their money without vetting it for quality. – Sam Varshavchik Sep 07 '22 at 11:41
  • @SamVarshavchik Oh, there are publishers that produce very bad stuff, and it doesn't even cost much these days if you only publish electronically (or even print on demand). You can make a lot of money from peddling books to those who can't tell that they are bad. (Even I have been approached about authoring by one of these publishers. They were basically churning out buzzword-compliant titles written by people who didn't mind, or didn't expect, not getting paid for their work.) – molbdnilo Sep 07 '22 at 12:25
  • That's precisely the reason for the link that I provided in my previous comment. – Sam Varshavchik Sep 07 '22 at 15:42

0 Answers0