Bug 71955 - Core dump and interesting behaviour while using reference class members
Summary: Core dump and interesting behaviour while using reference class members
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-07-21 08:54 UTC by Anand Kulkarni
Modified: 2016-07-21 09:24 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Attachment of the code. (7.13 KB, application/octet-stream)
2016-07-21 08:57 UTC, Anand Kulkarni
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Anand Kulkarni 2016-07-21 08:54:55 UTC
I have 3 programs attached p1,p2 and p3.cpp. p3 seems to dump core where as p1 and p2 work fine. seems like some compiler optimization is blowing it away as
per stack trace. gcc version is below. All 3 programs are same esentially 
with just commented code sections [ which are entirely independent logically ]

g++ -fpermissive -o p1.out p1.cpp
g++ -fpermissive -o p2.out p2.cpp
g++ -fpermissive -o p3.out p3.cpp

[anand@ldnpsr2937 gcc-question]$ gcc --version
gcc (GCC) 6.1.1 20160621 (Red Hat 6.1.1-3)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Comment 1 Anand Kulkarni 2016-07-21 08:57:05 UTC
Created attachment 38945 [details]
Attachment of the code.

code attached.
Comment 2 Martin Liška 2016-07-21 09:24:29 UTC
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()
 		{