I was solving a competitive programming task when I made a bug. I placed ios_base::sync_with_stdio(false), cin.tie(NULL)
into solve()
instead of main()
.
#include <bits/stdc++.h>
using namespace std;
void solve(){
ios_base::sync_with_stdio(false), cin.tie(NULL);
// implementation not important
}
int main() {
int t;
cin >> t;
while(t--)
solve();
}
This resulted in a memory error. However when I move it into the main()
I pass with no issues:
#include <bits/stdc++.h>
using namespace std;
void solve(){
// implementation not important
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int t;
cin >> t;
while(t--)
solve();
}
My understanding from this answer is that this disables the synchronization between C and C++ streams, and then unties cin
and cout
. Especially since I'm only using cin
/ cout
, why does moving that line around cause such a drastic difference?
(in case you want the actual source)