auto_ptr init question

K

kvnil

Any idea why would the following executable would crash (on Windows, VS2005
compiler)?

#include <memory>

struct C{};

int main(int argc, char** argv[])
{
std::auto_ptr<C> pC /*(new C())*/;
pC = new C();
return 0;
}

If the "new" part is move into the auto_ptr ctor (the commented part), the
exe terminates fine. Otherwise, it goes postal.

-- kvnil
 
I

Ian Collins

kvnil said:
Any idea why would the following executable would crash (on Windows,
VS2005 compiler)?

#include <memory>

struct C{};

int main(int argc, char** argv[])
{
std::auto_ptr<C> pC /*(new C())*/;
pC = new C();

This shouldn't compile.
return 0;
}

If the "new" part is move into the auto_ptr ctor (the commented part),
the exe terminates fine. Otherwise, it goes postal.
postal as in slow??
 
K

kvnil

kvnil said:
Any idea why would the following executable would crash (on Windows,
VS2005 compiler)?

#include <memory>

struct C{};

int main(int argc, char** argv[])
{
std::auto_ptr<C> pC /*(new C())*/;
pC = new C();
This shouldn't compile.

Why?? this is not a "const" auto_ptr? It was null at the beginning, and now
it is assigned an object. I don't
see the problem. Is there anything in the standard against this?
postal as in slow??

postal - crazy or insane.
 
A

Alf P. Steinbach

* kvnil:
Any idea why would the following executable would crash (on Windows,
VS2005 compiler)?

#include <memory>

struct C{};

int main(int argc, char** argv[])
{
std::auto_ptr<C> pC /*(new C())*/;
pC = new C();
return 0;
}

If the "new" part is move into the auto_ptr ctor (the commented part),
the exe terminates fine. Otherwise, it goes postal.

Hm, it shouldn't compile.

Checking...

Doesn't compile with either g++ 3.4.4 or msvc 7.1.


Cheers, & hth.,

- Alf
 
I

Ian Collins

kvnil said:
kvnil said:
Any idea why would the following executable would crash (on Windows,
VS2005 compiler)?

#include <memory>

struct C{};

int main(int argc, char** argv[])
{
std::auto_ptr<C> pC /*(new C())*/;
pC = new C();
This shouldn't compile.

Why?? this is not a "const" auto_ptr? It was null at the beginning, and
now it is assigned an object. I don't see the problem. Is there anything
in the standard against this?
Yes, std::auto_ptr<C> does not have an appropriate assignment operator
for a C*.
 
B

Barry

Any idea why would the following executable would crash (on Windows, VS2005
compiler)?

#include <memory>

struct C{};

int main(int argc, char** argv[])
{
std::auto_ptr<C> pC /*(new C())*/;
pC = new C();
return 0;

}

If the "new" part is move into the auto_ptr ctor (the commented part), the
exe terminates fine. Otherwise, it goes postal.

As marked by else threads, the code shouldn't compile against standard
conformed
compiler&library.

the problem here is that VC2005 use void other than T for
auto_ptr_ref.
VC9 has already fixed this.

try debug your code to see what's going on inside.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top