[Bug c++/32832] New: Seg fault on member function that does not return a val
The following code shows the error... #include #include #include #include #include using namespace std; class foo { public: vector bar (vector s) { //nothing returned but g++ never complains } }; int main() { string strs[] = {"a", "b"}; foo f; f.bar(vector(strs, strs + 2)); } -- [EMAIL PROTECTED]:~/working/TC$ g++ -o g_bug g_bug.cpp [EMAIL PROTECTED]:~/working/TC$ ./g_bug Segmentation fault -- #include #include #include #include #include using namespace std; class foo { public: vector bar (vector s) { return vector(); // fixes problem!? } }; int main() { string strs[] = {"a", "b"}; foo f; f.bar(vector(strs, strs + 2)); } - [EMAIL PROTECTED]:~/working/TC$ g++ -o g_bug g_bug.cpp [EMAIL PROTECTED]:~/working/TC$ ./g_bug - Note that with -Wall it warn, but will not report any error... - [EMAIL PROTECTED]:~/working/TC$ g++ -Wall -o g_bug g_bug.cpp g_bug.cpp: In member function std::vector, std::allocator >, std::allocator, std::allocator > > > foo::bar(std::vector, std::allocator >, std::allocator, std::allocator > > >): g_bug.cpp:12: warning: no return statement in function returning non-void g_bug.cpp:12: warning: control reaches end of non-void function I am using Ubuntu dapper with their version of gcc which I would assume to be pretty up to date. Sorry if this is a repeat, I didn't quite know how to search for this particular bug since it occurs at run time. -- Summary: Seg fault on member function that does not return a val Product: gcc Version: 4.0.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: CyrusOmega at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32832
[Bug c++/32832] Seg fault on member function that does not return a val
--- Comment #2 from CyrusOmega at gmail dot com 2007-07-20 11:56 --- Subject: Re: Seg fault on member function that does not return a val Is there ANY case where this action would NOT result in a segfault!? Specifically, it is segfaulting because something is being freed that was never created in the first place, or that has already been freed. If my code doesn't say what to return, then shouldn't the compiler either A) return the default object of the type the func will return or B) give an error telling me to fix the problem. I don't know what the spec says but this undefined behavior seems rather serious and should either be a default warning or the compiler should offer a consistent reaction. I am not bashing gcc in any way, just expression the opinion of a long timer coder that is trying to help make gcc even better. I will start using -Wall for everything ;) Thanks, Andrew On 20 Jul 2007 04:36:57 -, pinskia at gcc dot gnu dot org <[EMAIL PROTECTED]> wrote: > > > --- Comment #1 from pinskia at gcc dot gnu dot org 2007-07-20 04:36 > --- > This is only undefined behavior which is why we only warn about it. > > > -- > > pinskia at gcc dot gnu dot org changed: > >What|Removed |Added > > Status|UNCONFIRMED |RESOLVED > Resolution||INVALID > > > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32832 > > --- You are receiving this mail because: --- > You reported the bug, or are watching the reporter. > -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32832
[Bug c++/32934] New: No warning when creating a non const derived funtion from a const virtual funciton
Maybe this isn't a bug, but I can't see why this shouldn't at least be a warning since it changes the output of the program... ==C++ Source with B.print not const #include class A { public: virtual char * print() const { return "A\n";} virtual ~A(){}; }; class B : public A { public: virtual char * print() { return "B\n";} virtual ~B(){}; }; int main () { B b; A & a = b; std::cout << a.print() << b.print(); } [EMAIL PROTECTED]:~/working$ g++ -Wall -o gcc_bug gcc_bug.cpp [EMAIL PROTECTED]:~/working$ ./gcc_bug A B =C++ Source with B.print const= #include class A { public: virtual char * print() const { return "A\n";} virtual ~A(){}; }; class B : public A { public: virtual char * print() const { return "B\n";} virtual ~B(){}; }; int main () { B b; A & a = b; std::cout << a.print() << b.print(); } [EMAIL PROTECTED]:~/working$ g++ -Wall -o gcc_bug gcc_bug.cpp [EMAIL PROTECTED]:~/working$ ./gcc_bug B B What am I missing? Andrew -- Summary: No warning when creating a non const derived funtion from a const virtual funciton Product: gcc Version: 4.0.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: CyrusOmega at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32934
[Bug c++/32934] No warning when creating a non const derived function from a const virtual function
--- Comment #2 from CyrusOmega at gmail dot com 2007-07-30 12:04 --- (In reply to comment #1) > Well it is valid as B::print hides A::print. > Try doing: > #include > class A { > public: > virtual char * print() const { return "A\n";} > virtual ~A(){}; > }; > class B : public A { > public: > virtual char * print() { return "B\n";} > using A::print; > virtual ~B(){}; > }; > > int main () > { > B b; > A & a = b; > const B &b1 = b; > std::cout << a.print() << b1.print(); > } > > Which shows that b1.print will call A::print. If you don't include using > A::print, then A::print wil be hidden in B. > The problem I am seeing is that I want to inherit from a virtual const function but I don't want MY derived function to be const. Just because my parent class thought the member should be const doesn't mean I agree, or am I simply restricted? Thanks, and again I am sorry if this is simply a lack of my understanding of C++. Andrew -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32934
[Bug c++/32934] No warning when creating a non const derived function from a const virtual function
--- Comment #4 from CyrusOmega at gmail dot com 2007-07-31 21:41 --- Subject: Re: No warning when creating a non const derived function from a const virtual function Wow, thanks. I thought that -Wall was ALL warnings. grumble grumble... Andrew On 31 Jul 2007 21:32:32 -, fang at csl dot cornell dot edu <[EMAIL PROTECTED]> wrote: > > > --- Comment #3 from fang at csl dot cornell dot edu 2007-07-31 21:32 > --- > Try compiling with -Woverloaded-virtual (C++ only). That catches the > situation > you describe. (I don't think it's enabled by -Wall or -Wextra.) > > > -- > > fang at csl dot cornell dot edu changed: > >What|Removed |Added > > CC||fang at csl dot cornell dot >||edu > > > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32934 > > --- You are receiving this mail because: --- > You reported the bug, or are watching the reporter. > -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32934