how to use boost::shared_ptr?
I have a class member (boost::shared_ptr): class XYZ { private: boost::shared_ptr<int> m_pBuff; // other member ... public: void func(int n) { m_pBuff.reset(new int[n]); // or m_pBuff = boost::shared_ptr<int>(new int[n]) ...; } // other functions ... } The func(n) will be called many times inside / outside the class. Can I use shared_ptr here like: m_pBuff.reset(new int[n]); or m_pBuff = boost::shared_ptr<int>(new int[n]); I tried both and get error said heap corruption. -- View this message in context: http://boost.2283326.n4.nabble.com/how-to-use-boost-shared-ptr-tp4642120.htm... Sent from the Boost - Users mailing list archive at Nabble.com.
2013/2/1 young
I have a class member (boost::shared_ptr):
class XYZ { private: boost::shared_ptr<int> m_pBuff; // other member ... public: void func(int n) { m_pBuff.reset(new int[n]); // or m_pBuff = boost::shared_ptr<int>(new int[n]) ...; } // other functions ... }
The func(n) will be called many times inside / outside the class. Can I use shared_ptr here like: m_pBuff.reset(new int[n]); or m_pBuff = boost::shared_ptr<int>(new int[n]);
I tried both and get error said heap corruption
The error is passing an array to a shared_ptr. Take a look at shared_array ;-) HTH, Kris
Can I assign a array to a shared_ptr pointer? -- View this message in context: http://boost.2283326.n4.nabble.com/how-to-use-boost-shared-ptr-tp4642120p464... Sent from the Boost - Users mailing list archive at Nabble.com.
On 1 February 2013 16:46, young
Can I assign a array to a shared_ptr pointer?
Read the docs, Luke! "Normally, a shared_ptr cannot correctly hold a pointer to a dynamically allocated array. See shared_array for that usage." http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/shared_ptr.htm And, don't forget to follow the link there and read this one too: http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/shared_array.htm Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
After I changed to shared_array, same error. by the way, can I assign an array to a shared_ptr pointer? -- View this message in context: http://boost.2283326.n4.nabble.com/how-to-use-boost-shared-ptr-tp4642120p464... Sent from the Boost - Users mailing list archive at Nabble.com.
Hello young
shared_ptr holds pointers to dynamically allocated arrays. shared_ptr
uses "delete ptr;" to clean-up when the pointer is no longer
referenced/goes out of scope. Use shared_array or scoped_array, which
correctly use delete[] ptr.
On Fri, Feb 1, 2013 at 11:05 AM, young
I have a class member (boost::shared_ptr):
class XYZ { private: boost::shared_ptr<int> m_pBuff; // other member ... public: void func(int n) { m_pBuff.reset(new int[n]); // or m_pBuff = boost::shared_ptr<int>(new int[n]) ...; } // other functions ... }
The func(n) will be called many times inside / outside the class. Can I use shared_ptr here like: m_pBuff.reset(new int[n]); or m_pBuff = boost::shared_ptr<int>(new int[n]);
I tried both and get error said heap corruption.
-- View this message in context: http://boost.2283326.n4.nabble.com/how-to-use-boost-shared-ptr-tp4642120.htm... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Didier JP GF 15 Rue Albert Roussel 75017 Paris skype : bdidierjp
On 1 February 2013 17:33, Didier J-P Guion Firmin
Hello young
shared_ptr holds pointers to dynamically allocated arrays.
s/holds/does not hold/ Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
participants (4)
-
Didier J-P Guion Firmin
-
Krzysztof Czainski
-
Mateusz Loskot
-
young