473,382 Members | 1,705 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,382 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 3196
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.