[Bug c++/71955] Core dump and interesting behaviour while using reference class members

marxin at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Jul 21 09:24:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71955

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |marxin at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #2 from Martin Liška <marxin at gcc dot gnu.org> ---
Also p1 and p2 contain undefined behavior, that's reason why the program can't
be compiled w/ -fpermissive.

Running p1 with valgind shows:

==1728== Conditional jump or move depends on uninitialised value(s)
==1728==    at 0x4F5D323: std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char>
>::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&) (in /usr/lib64/libstdc++.so.6.0.22)
==1728==    by 0x4F5D6E8: std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char>
>::operator=(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&) (in /usr/lib64/libstdc++.so.6.0.22)
==1728==    by 0x400D85:
SampleStringClass::SampleStringClass(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&) (in
/home/marxin/Downloads/gcc-question/a.out)
==1728==    by 0x400CA5: main (in /home/marxin/Downloads/gcc-question/a.out)
==1728== 
==1728== Use of uninitialised value of size 8
==1728==    at 0x4EF12C0: std::basic_ostream<char, std::char_traits<char> >&
std::operator<< <char, std::char_traits<char>, std::allocator<char>
>(std::basic_ostream<char, std::char_traits<char> >&,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
const&) (in /usr/lib64/libstdc++.so.6.0.22)
==1728==    by 0x400DA9: SampleStringClass::show() (in
/home/marxin/Downloads/gcc-question/a.out)
==1728==    by 0x400CB1: main (in /home/marxin/Downloads/gcc-question/a.out)
==1728== 
Anand Apparao Kulkarni
==1728== Use of uninitialised value of size 8
==1728==    at 0x4EF12C0: std::basic_ostream<char, std::char_traits<char> >&
std::operator<< <char, std::char_traits<char>, std::allocator<char>
>(std::basic_ostream<char, std::char_traits<char> >&,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
const&) (in /usr/lib64/libstdc++.so.6.0.22)
==1728==    by 0x400DA9: SampleStringClass::show() (in
/home/marxin/Downloads/gcc-question/a.out)
==1728==    by 0x400CBD: main (in /home/marxin/Downloads/gcc-question/a.out)
==1728== 
Anand Apparao Kulkarni


Thus I would suggest following patch to your source code:

--- p1.backup   2016-07-21 11:22:20.651299644 +0200
+++ p1.cpp      2016-07-21 11:23:37.128793125 +0200
@@ -5,11 +5,10 @@

 class SampleStringClass {
        private:
-               std::string& ref;
+               const std::string& ref;
        public:
-               SampleStringClass(const std::string& arg)
+               SampleStringClass(const std::string& arg): ref (arg)
                {
-                       ref=arg;
                }
                void show()
                {
@@ -19,11 +18,10 @@

 class SampleIntClass {
        private:
-               int& ref;
+               const int& ref;
        public:
-               SampleIntClass(const int& arg)
+               SampleIntClass(const int& arg): ref (arg)
                {
-                       ref=arg;
                }
                void show()
                {


More information about the Gcc-bugs mailing list