6

Very Strange behavior happened to me. I am using latest Cygwin32, Cygwin64 and MinGW32 with GCC 4.9.2 , 4.9.2 and 4.8.1 respectively on Windows 7 64-bit. I am testing also on 32-bit Linux using GCC 4.8.2.

So on all systems this works

#include <bits/stdc++.h>
using namespace std;
string s,t;
int main(){
  cin>>s>>t;
  cout<<s;
}

and this works

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
string s="a",t="b";
int main(){
    cin>>s>>t;
    cout<<s;
}

but the next one crashes on Windows after inputting the first string on the 3 configurations mentioned above, but works correctly on Linux:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
string s,t;
int main(){
    cin>>s>>t;
    cout<<s;
}

Clearly the only difference is that the string is empty before inputting, and _GLIBCXX_DEBUG is enabled.

First, is this a reproducible problem? i.e. does it happen on every PC with the same configuration , or only my PC? Second, What is the problem? What should I do? I clearly need _GLIBCXX_DEBUG to debug other STL structures in the code.

yugr
  • 19,769
  • 3
  • 51
  • 96
Mohamed El-Nakeep
  • 6,580
  • 4
  • 35
  • 39
  • 1
    I observed the same behavior. – Oswald Feb 25 '15 at 00:19
  • Does the same thing happen if you include `` and `` separately rather than ``? – user657267 Feb 25 '15 at 00:23
  • 1
    I am very suspecious of the non standard header `#include `. Would prefer a test that used `#include ` and `#include ` and did not use `using namespace std;` – Martin York Feb 25 '15 at 00:24
  • 2
    @user657267 The same happens if I remove #include and use and separately – Mohamed El-Nakeep Feb 25 '15 at 00:33
  • 1
    @LokiAstari The same happens if I remove using namespace std; and use std::string and std::string – Mohamed El-Nakeep Feb 25 '15 at 00:34
  • 1
    Could you try the [nuwen MinGW](http://nuwen.net/mingw.html) and post the results? It works for me. – Baum mit Augen Feb 25 '15 at 01:04
  • 1
    @BaummitAugen I have just tried nuwen MinGW , and it works beautifully without crash as Linux , on the contrary of official MinGW32, Cygwin32 and Cygwin64. – Mohamed El-Nakeep Feb 25 '15 at 05:42
  • "What should I do?" Please file a bug report, as usual when you find a bug. – Marc Glisse Feb 25 '15 at 07:31
  • @MuhammadAnnaqeeb I had a vastly better experience with the nuwen distro so far, and you appear to have hit a bug in those other distros you mentioned. You should file a bug report for that. But good to see you got it working. – Baum mit Augen Feb 25 '15 at 13:22
  • @MarcGlisse Where should I file a bug report? and to which project? – Mohamed El-Nakeep Feb 25 '15 at 15:48
  • Reporting it to both cygwin (https://cygwin.com/problems.html) and mingw (http://www.mingw.org/reporting_bugs) makes sense to me, but reporting direcly to gcc (https://gcc.gnu.org/bugzilla/) should be fine as well. – Marc Glisse Feb 25 '15 at 16:19
  • @MarcGlisse as discussed in http://lists.apple.com/archives/cocoa-dev/2009/Sep/msg01096.html and http://lists.freedesktop.org/archives/libreoffice/2011-June/014417.html the same problem happens on OS X with gcc 4.2 ; these posts are from 2009 and 2011 respectively and the problem happens to both getline and cin. May be the bug is reported already. – Mohamed El-Nakeep Feb 27 '15 at 00:08
  • same bug but on OS X http://stackoverflow.com/questions/1962685/xcode-stl-c-debug-compile-error – Mohamed El-Nakeep Feb 27 '15 at 00:13
  • 3
    Looking at mingw: http://sourceforge.net/p/mingw/bugs/1666/ they say to report to gcc directly. Looking at gcc, the closest I find are: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54173 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53838 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=33021 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64504 but they all involve at least one other option (-fwhole-program, -fvisibility). In any case, std::string was completely rewritten for gcc-5. – Marc Glisse Feb 27 '15 at 07:39
  • @MarcGlisse I believe the problem is caused by different ABI under `_GLIBCXX_DEBUG`. I was struck by this in past. – yugr Oct 21 '18 at 12:39

1 Answers1

1

_GLIBCXX_DEBUG changes ABI of STL classes (i.e. layout of structs, including streams and std::string):

To use the libstdc++ debug mode, compile your application with the compiler 
flag -D_GLIBCXX_DEBUG. Note that this flag changes the sizes and behavior of 
standard class templates such as std::vector, and therefore you can only link 
code compiled with debug mode and code compiled without debug mode if no 
instantiation of a container is passed between the two translation units.

(from official docs).

Your app (compiled with _GLIBCXX_DEBUG) passes std::string to std::cin::operator >> in libstdc++.dll (compiled without this macro) and thus violates the requirement above. Such violations normally manifest in runtime crashes: see this or this for concrete examples.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • Libstdc++ does not document that you have to recompile libstdc++ for debug mode (they do for the thread sanitizer or other options), so I think it is not supposed to cause a conflict. – Marc Glisse Oct 21 '18 at 22:58
  • @MarcGlisse Thanks, I added a link to STL docs (OP is passing `string` across incompatible modules). – yugr Oct 22 '18 at 08:47