patterncppCritical
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);
Viewed 0 times
tiecinsync_with_stdiosignificancefalseios_basenull
Problem
What is the significance of including this in C++ programs?
In my tests, it speeds up the execution time, but is there a test case I should be worried about by including this?
Do the 2 statements always have to be together, or is the first one sufficient, i.e. omitting
Also, is it permissible to use simultaneous C stdio and C++ iostream functions if
This below test-case worked fine until I used
(https://www.codechef.com/viewsolution/7316085)
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);In my tests, it speeds up the execution time, but is there a test case I should be worried about by including this?
Do the 2 statements always have to be together, or is the first one sufficient, i.e. omitting
std::cin.tie(nullptr)?Also, is it permissible to use simultaneous C stdio and C++ iostream functions if
sync_with_stdio has been set to false?This below test-case worked fine until I used
scanf/printf in a C++ program with the value as true. In this case, it gave a segmentation fault. What could be the possible explanation for this?#include
#include
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int w;
scanf("%d",&w);
char crap=getchar();
while(w--){
string a;
getline(cin,a);
int t=1,i;
if((a.length()==1)||(a.length()==2))
printf("Bad\n");
else{
for(i=0;i<a.length()-2;i++){
if(((a[i]=='0')&&(a[i+1]=='1')&&(a[i+2]=='0'))||((a[i]=='1')&&(a[i+1]=='0')&&(a[i+2]=='1'))){
t=1;
break;
}
else t=0;
}
if(t==0)
printf("Bad\n");
else printf("Good\n");
}
}
return 0;
}(https://www.codechef.com/viewsolution/7316085)
Solution
The two calls have different meanings that have nothing to do with performance; the fact that it speeds up the execution time is (or might be) just a side effect. You should understand what each of them does and not blindly include them in every program because they look like an optimization.
This disables the synchronization between the C and C++ standard streams. By default, all standard streams are synchronized, which in practice allows you to mix C- and C++-style I/O and get sensible and expected results. If you disable the synchronization, then C++ streams are allowed to have their own independent buffers, which makes mixing C- and C++-style I/O an adventure.
Also keep in mind that synchronized C++ streams are thread-safe (output from different threads may interleave, but you get no data races).
This unties
By default
If
So if you untie
In conclusion, know what each of them does, understand the consequences, and then decide if you really want or need the possible side effect of speed improvement.
ios_base::sync_with_stdio(false);This disables the synchronization between the C and C++ standard streams. By default, all standard streams are synchronized, which in practice allows you to mix C- and C++-style I/O and get sensible and expected results. If you disable the synchronization, then C++ streams are allowed to have their own independent buffers, which makes mixing C- and C++-style I/O an adventure.
Also keep in mind that synchronized C++ streams are thread-safe (output from different threads may interleave, but you get no data races).
cin.tie(NULL);This unties
cin from cout. Tied streams ensure that one stream is flushed automatically before each I/O operation on the other stream.By default
cin is tied to cout to ensure a sensible user interaction. For example:std::cout > name;If
cin and cout are tied, you can expect the output to be flushed (i.e., visible on the console) before the program prompts input from the user. If you untie the streams, the program might block waiting for the user to enter their name but the "Enter name" message is not yet visible (because cout is buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).So if you untie
cin from cout, you must make sure to flush cout manually every time you want to display something before expecting input on cin.In conclusion, know what each of them does, understand the consequences, and then decide if you really want or need the possible side effect of speed improvement.
Code Snippets
ios_base::sync_with_stdio(false);cin.tie(NULL);std::cout << "Enter name:";
std::cin >> name;Context
Stack Overflow Q#31162367, score: 518
Revisions (0)
No revisions yet.