473,320 Members | 1,940 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,320 software developers and data experts.

Stupid problem with boost::share_ptr

Is there something wrong with this code?

#include <boost/shared_ptr.hpp>

int main()
{
boost::shared_ptr<double> g(new double);
boost::shared_ptr<double> f(new double);

f = boost::shared_ptr<double>(g.get());

return 0;
}

I get a run-time memory error when I compile it with gcc. Did I do
something wrong?

Jan 9 '06 #1
2 3193
Roman Werpachowski wrote:
Is there something wrong with this code?

#include <boost/shared_ptr.hpp>

int main()
{
boost::shared_ptr<double> g(new double);
boost::shared_ptr<double> f(new double);

f = boost::shared_ptr<double>(g.get());
// You can't do this.
return 0;
}

I get a run-time memory error when I compile it with gcc. Did I do
something wrong?


g owns the first allocated double.
Then you make f also own the first allocated double.

When g and f go out of scope, both destructors delete the original
allocated double, which is undefined behaviour.

You did this to yourself, to be honest, whats wrong with:

f = g;

In the assignment, the count for the first double is decremented,
reaches 0 and it is deleted, and the count for the second double is
incremented. You then have 2 references to the second double,
accessible through f or g.

What are you trying to achieve?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 9 '06 #2
I was just fooling around with boost, that's all ;-)

Jan 9 '06 #3

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

Similar topics

29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
0
by: Roman Werpachowski | last post by:
Is there something wrong with this code? #include <boost/shared_ptr.hpp> int main() { boost::shared_ptr<double> g(new double); boost::shared_ptr<double> f(new double); f =...
3
by: Noah Roberts | last post by:
I am trying to pull behavior out of a class so that the class can be replaced. Part of that behavior takes place in the destructor of the object. I need call sites to change so that they call a...
7
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
6
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
5
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
9
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.