473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about auto_ptr

Below code has output

Foo created 3
0
doit
Foo destructed 3

I am confused. When an auto_ptr object is assigned to another auto_ptr
object, the control of pointer is transferred from orginal auto_ptr to
target auto_ptr, and orginal auto_ptr points to NULL. Then foo->doit()
should throw a Segmentation Fault in my mind, but the code works fine
just like foo still has the reference.

/* Code */
#include <memory>
class Foo
{
public:
Foo(int v):_v(v) { printf("Foo created %d\n", v); }
~Foo() { printf("Foo destructed %d\n",_v); }

void doit();
int _v;
};

void Foo::doit()
{
printf("doit\n");
}
int main(int argc, char** argv)
{
Foo *f = new Foo(3);
std::auto_ptr<Foo> foo (f);
std::auto_ptr<Foo> foo2 = foo;

printf("%d\n", foo.get());
foo->doit();//this line should cause Segmentation Fault IMO

return 0;
}
Jul 22 '05 #1
2 1758

"Morgan Cheng" <mo************@gmail.com> wrote in message
news:cp**********@avnika.corp.mot.com...
I am confused. When an auto_ptr object is assigned to another auto_ptr
object, the control of pointer is transferred from orginal auto_ptr to
target auto_ptr, and orginal auto_ptr points to NULL. Then foo->doit()
should throw a Segmentation Fault in my mind,
The standard just says that the result of invoking foo->doit() is undefined; it
doesn't mandate a seg fault or any other behavior. However, if you wan't a seg
fault, you'll probably have better luck if the implementation of doit()
references non-static member data of the class Foo.
but the code works fine
just like foo still has the reference.
/* Code */
#include <memory>
class Foo
{
public:
Foo(int v):_v(v) { printf("Foo created %d\n", v); }
~Foo() { printf("Foo destructed %d\n",_v); }

void doit();
int _v;
};

void Foo::doit()
{
printf("doit\n");
try

std::cout << "_v = " << _v << "\n";
}
int main(int argc, char** argv)
{
Foo *f = new Foo(3);
std::auto_ptr<Foo> foo (f);
std::auto_ptr<Foo> foo2 = foo;

printf("%d\n", foo.get());
foo->doit();//this line should cause Segmentation Fault IMO

return 0;
}


Jonathan
Jul 22 '05 #2
* Morgan Cheng:

void Foo::doit()
{
printf("doit\n");
}

int main(int argc, char** argv)
{
Foo *f = new Foo(3);
std::auto_ptr<Foo> foo (f);
std::auto_ptr<Foo> foo2 = foo;

printf("%d\n", foo.get());
foo->doit();//this line should cause Segmentation Fault IMO

return 0;
}


No.

There are two issues here: (i) what the standard has to say about this,
and (ii) a reasonable explanation for the fact that doit() is called.

Regarding (i) you have Undefined Behavior, which means the program can
do absolutely anything -- that it ends up calling doit(), on your
implementation, compiler switches etc., is therefore perfectly okay.

Regarding (ii) a reasonable explanation is that doit() is not virtual
and does not use any member variables or virtual functions, so there's
nothing that would cause a reasonable implementation to access an
invalid memory address -- unless the compiler emits extra code to
check for that, which if possible probably requires a special switch.

Btw., also the first printf() in main is Undefined Behavior: a pointer
will not necessarily fit into an int, so you could be corrupting the
stack. At least _cast_ the pointer to int first if you absolutely must
printf it as a decimal number! But better, use std::cout.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3

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

Similar topics

0
by: Prune Tracy | last post by:
Hello. As practice in programming using templates, and for interest's sake, I wrote some code to see how floating point numbers are stored on my system. I hoped to be able to use it like: ...
10
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. #...
10
by: Lloyd Dupont | last post by:
how do I redefine the new operator? for all the structure I use at once! (some of them comes from C include files). The rationale: I'm writting a managed C++ wrapper around C API (external...
8
by: Lloyd Dupont | last post by:
I try to use the auto_ptr in my code but that doesn't work. even std::auto_ptr. I guess I have to add an #include statement, but I can't figure out the right file to add. Also I want to use...
14
by: kathy | last post by:
I have a vector which I save the pointer. function() { std::vector <CMyData*> vpMyData; vpMyData.push_back(new CMyData(0)); vpMyData.push_back(new CMyData(1)); vpMyData.push_back(new...
2
by: flopbucket | last post by:
I saw the following post a few messages back and am trying to understand exactly why this is so. Thanks for the information. My comments begin with **. The original post was arguing that STL...
16
by: Yong Hu | last post by:
Does the smart pointer never lead to memory leak? Thanks,
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
2
by: dolphin | last post by:
Hi All Today , I read the source code of auto_ptr. I have a question about the copy constructor of auto_ptr. template<class _Ty> class auto_ptr { .................. } there is a copy...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...

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.