473,804 Members | 4,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bug: stack overflow in std::auto_ptr

Hi
Please try this code. I think that it's perfectly legal. However when
compiled under MSVC71 stack overflow happens in first line of main, thus
second line is never executed.
B.

#include <iostream>
#include <memory>

using namespace std;

struct A
{
virtual ~A() {}
};

struct B : public A
{
};

auto_ptr<B> test()
{
return auto_ptr<B>(new B);
};

int main()
{
auto_ptr<A> r = test();
std::cout << "You won't see this!" << std::endl;
}
Nov 17 '05 #1
20 3740
Bronek,

Compiling your code with VC7.1 gives this warning:

warning C4717: 'std::auto_ptr< B>::operator<A > std::auto_ptr_r ef<A>' :
recursive on all control paths, function will cause runtime stack
overflow

.... which is what's happening to you in practice.

The online Comeau compiler won't compile your code, saying:

""ComeauTest.c" , line 23: error: class "std::auto_ptr< A>" has no
suitable copy constructor
auto_ptr<A> r = test();
"

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
On Thu, 22 Jan 2004 14:49:54 +0000, David Lowndes wrote:
warning C4717: 'std::auto_ptr< B>::operator<A > std::auto_ptr_r ef<A>' :
recursive on all control paths, function will cause runtime stack
overflow

... which is what's happening to you in practice.
.... and puts us in the realm of Undefined Behaviour.
""ComeauTest.c" , line 23: error: class "std::auto_ptr< A>" has no
suitable copy constructor
auto_ptr<A> r = test();


which means that code is illegal. Is it ? Anyway I'd say that
"std::auto_ ptr is flawed and has to be fixed" :((
B.
Nov 17 '05 #3
>> ""ComeauTest.c" , line 23: error: class "std::auto_ptr< A>" has no
suitable copy constructor
auto_ptr<A> r = test();
which means that code is illegal. Is it ?


Not being intimately familiar with the standards I can't say - but the
Comeau compiler is usually right.
Anyway I'd say that
"std::auto_p tr is flawed and has to be fixed" :((


From what I've read about std::auto_ptr, it has many gotchas and can
be more trouble than its worth.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #4
David Lowndes <da****@mvps.or g> wrote:
Bronek,

Compiling your code with VC7.1 gives this warning:

warning C4717: 'std::auto_ptr< B>::operator<A > std::auto_ptr_r ef<A>' :
recursive on all control paths, function will cause runtime stack
overflow
Funny. From VC I get this:

test.cpp(22) : error C2664: 'std::auto_ptr< _Ty>::auto_ptr( std::auto_ptr<_ Ty> &) throw()' :
cannot convert parameter 1 from 'std::auto_ptr< _Ty>' to 'std::auto_ptr< _Ty> &'
[...]

The online Comeau compiler won't compile your code, saying:

""ComeauTest.c" , line 23: error: class "std::auto_ptr< A>" has no
suitable copy constructor
auto_ptr<A> r = test();
"

while Comeau here says this:

Comeau C/C++ 4.3.3 (Aug 10 2003 15:39:53) for _MS_WINDOWS_x86 _Beta8
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:non-strict warnings microsoft C++

"test.cpp", line 22: warning: initial value of reference to non-const must be
an lvalue
auto_ptr<A> r = test();
^
However, if I change this line

auto_ptr<A> r = test();

into this one

auto_ptr<A> r( test() );

I get your warning with VC7.1, and

initial value of reference to non-const must be an lvalue

from Comeau.
Now, when I tried this

auto_ptr<B> r = test();
auto_ptr<A> s(r);

both Comeau and VC7.1 accept the code.
I'm not sure what to make of this, but it
seems to be a work-around.
If you want to know chapter and verse of
the standard, I'd suggest you ask in either
comp.lang.c++.m oderated or comp.std.c++. I
have decided long ago that the std doc is
far beyond my abilities to digest English.
Dave

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #5
> Funny. From VC I get this:

test.cpp(22) : error C2664: 'std::auto_ptr< _Ty>::auto_ptr( std::auto_ptr<_ Ty> &) throw()' :
cannot convert parameter 1 from 'std::auto_ptr< _Ty>' to 'std::auto_ptr< _Ty> &'
Mighty odd that we've got different results :(

If I get time I'll have to check what I tried again.
I have decided long ago that the std doc is far beyond my abilities to digest English.

<

Mine too.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #6
> Funny. From VC I get this:

test.cpp(22) : error C2664: 'std::auto_ptr< _Ty>::auto_ptr( std::auto_ptr<_ Ty> &) throw()' :
cannot convert parameter 1 from 'std::auto_ptr< _Ty>' to 'std::auto_ptr< _Ty> &'


I've tried again and VC7.1 (version 13.10.3077) reports the "recursive
on all control paths, function will cause runtime stack overflow"
warning for me.

The Comeau online (version 4.3.3) error is as I originally posted too.

I wonder what we have done differently?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #7
On Fri, 23 Jan 2004 22:44:32 +0000, David Lowndes wrote:
I've tried again and VC7.1 (version 13.10.3077) reports the "recursive
on all control paths, function will cause runtime stack overflow"
warning for me.

The Comeau online (version 4.3.3) error is as I originally posted too.

I wonder what we have done differently?


Maybe that question should be passed to Mr. Plauger ?
B.
Nov 17 '05 #8
Bronek Kozicki <br**@rubikon.p l> wrote:
On Fri, 23 Jan 2004 22:44:32 +0000, David Lowndes wrote:
[David, haven't seen your posting, so I
don't know if you wrote anything else
which I missed.]
I've tried again and VC7.1 (version 13.10.3077) reports the "recursive
on all control paths, function will cause runtime stack overflow"
warning for me.
That's funny.
I tried it again: Copy & paste the code
from Bronek's original posting int a my
test project, compile it, and get this:

------ Build started: Project: Test, Configuration: Debug Win32 ------

Compiling...
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
cl /Od /I "..\.." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Gm /EHsc /RTC1 /MLd /GS /Za /Zc:wchar_t /Zc:forScope /GR
/Fo"C:\Temp\Test \Debug\Test/" /Fd"C:\Temp\Test \Debug\Test/vc70.pdb" /W4 /c /Wp64 /Zi /TP
\Develop\test.c pp
test.cpp
\Develop\test.c pp(22) : error C2664: 'std::auto_ptr< _Ty>::auto_ptr( std::auto_ptr<_ Ty> &) throw()' : cannot convert parameter 1
from 'std::auto_ptr< _Ty>' to 'std::auto_ptr< _Ty> &'
with
[
_Ty=A
]
and
[
_Ty=A
]
and
[
_Ty=A
]
A reference that is not to 'const' cannot be bound to a non-lvalue

[Damn, I hate the IDE trying to copy formatted text
to the clipboard! OE is just to stupid to deal with
it. Can I have a switch to turn this off, please?!]

My "yvals.h" file has a

#define _CPPLIB_VER 313

in it.
The Comeau online (version 4.3.3) error is as I originally posted too.
Yeah:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATI ON_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest .c", line 18: error: extra ";" ignored,
In C: A function definition does not end with a semicolon
In C++: A function definition, extern "C" or namespace, does not end with a semicolon
};
^

"ComeauTest .c", line 22: error: class "std::auto_ptr< A>" has no suitable copy
constructor
auto_ptr<A> r = test();
^

2 errors detected in the compilation of "ComeauTest .c".

I wonder what we have done differently?

For Comeau I know. I was using this:

Comeau C/C++ 4.3.3 (Aug 10 2003 15:39:53) for _MS_WINDOWS_x86 _Beta8
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:non-strict warnings microsoft C++

That's what I have on disk, and I think it's
using VC's std lib. AFAIK, Comeau Online uses
libcomo, which is a derivate of SGI's std lib.
Maybe that question should be passed to Mr. Plauger ?
This might be true for the difference between
Comeau Online and my version, which does use
Plauger's std lib. (You might want to post
your question in the STL group. Or go and ask
on comp.lang.c++.m oderated ot comp.std.c++.)
However, I am still puzzled why David gets
different results with VC.
B.


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #9
> That's funny.

OK, I see what the difference is now - you have the /Za "Disable
Language Extensions" compiler options set.

If I change that setting in my test project, I get the same error as
you.

I'd tested the code by creating a Win32 console application and
pasting the code it.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17137
by: SerGioGio | last post by:
Hello ! When a ref. counted smart pointer (template) class is available (like boost::shared_ptr), is there any (semantic) reason to still use std::auto_ptr rather than this smart pointer ? Or should one use shared_ptr for every cases ? Thanks in advance for your remarks ! SerGioGio
2
2114
by: Jamie Burns | last post by:
Hello, I am trying to write some threadsafe code. I am using mutexes to restrict access to certain class members. Consider the method below: // begin code nippet bool rControl::getVisibility() { {
9
2255
by: BekTek | last post by:
How do you think? and why?
10
2550
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. # include <iostream> # include <memory> class CallbackBase { public: virtual void operator()() const { };
1
2852
by: Guido Forthofer | last post by:
Hello, I convert my VC6 C++ project after VC7 (7.1) and have now an compiler error d:\Programme\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810): error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or copy constructor is declared 'explicit' with
8
2653
by: Marchello | last post by:
Hi all. I have a class with virtual functions. This class lives in DLL. In main program I use this class by obtaining his pointer (by function that's exports from dll). class CfromDLL { ... virtual int Foo(); ...
10
2639
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
7
2015
by: j.l.olsson | last post by:
Hello, I am using std::auto_ptr to try to keep ownership of resources straight. However, I became aware that exception objects must provide a copy constructor, which made me uncertain of the soundness of throwing std::auto_ptrs... It was explained to me that a possible problem case is: try {
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.