- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I am having an issue, I really can't find an answer to. With the following code, I get a segmentation fault. But only for -O2 / -O3. -O0 and -O1 are fine.
The fault does occur with the C++11 initialization of the string member additionalMessage = ""; If this is not pre-set, it works just fine.
When I replace the member with char*, the code works as expected.
Since the same error occurs with GCC, I guess it is working as intended - but as stated before, I don't come up with the explenation... I could just set additionalMessage to "" in the constructor, but I just want to know what is happening.
For any clues, I am thankful!
Greetings from Germany,
Hendrik
---------
#include <cstdlib> #include <iostream> #include <string> #include <exception> using namespace std; class genericException : public std::exception{ public: genericException(){ } virtual char const* what(int lang) const throw () = 0; protected: int additionalId = 0; std::string additionalMessage = ""; // does not work } ; class ecTest01 : public genericException{ public: const char* message[2] = {"Message EN 111", "Message DE 111"}; ecTest01() : genericException(){ } char const* what() const throw(){ return message[0]; } char const* what(int lang) const throw(){ return message[lang]; } }; int main(int argc, char** argv){ try{ throw ecTest01(); }catch(genericException& e){ std::cout << e.what(0) << std::endl; } return 0; }
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I cannot reproduce this issue with gcc and icc. which GCC version you are using? (as you mentioned it also crash with gcc). I guess it is something wrong with your gcc? I've tried with both gcc 4.9.0 and gcc 4.8.2.
$ g++ temp.cpp -O2 -std=c++11 $ ./a.out Message EN 111 $ g++ temp.cpp -O1 -std=c++11 $ ./a.out Message EN 111 $ g++ temp.cpp -O3 -std=c++11 $ ./a.out Message EN 111 $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/opt/gcc/gcc-4.9.0/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: ../gcc-4.9.0/configure --prefix=/opt/gcc/gcc-4.9.0 --disable-nls --enable-languages=c,c++ Thread model: posix gcc version 4.9.0 (GCC) $
Thanks,
Shenghong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Shenghong,
Thank you for helping me out so quick, too bad there is no conclusive answer.
I am using G++ 4.8.3 (Red Hat 4.8.3-9). But this was just a cross test. My main problem was it not working on the icpc.
But if it works for you, I am really out of clues again... since the error occurs only for strings it might be the stringfwd.h which is broken?! But again, my C++ lib folder 4.8.3 in /usr/include/c++ just is a link to 4.8.2 which is the version it worked for you.
Regards,
Hendrik

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page