question about boost pointer
I have question about boost pointer. If I define a pointer by using: boost::shared_ptr<int> p; How to use p to get next int? In classic c++ pointer, I could use *(p+1). How to do it in Boost? -- View this message in context: http://boost.2283326.n4.nabble.com/question-about-boost-pointer-tp4631069.ht... Sent from the Boost - Users mailing list archive at Nabble.com.
You wouldn't have a next int as that declaration gets you a single int.
See http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/sp_techniques.html,
section "Using a shared_ptr to hold a pointer to an array", and the
link therein.
Best,
Dee
On Fri, Jun 8, 2012 at 9:45 PM, young
I have question about boost pointer. If I define a pointer by using:
boost::shared_ptr<int> p;
How to use p to get next int? In classic c++ pointer, I could use *(p+1). How to do it in Boost?
-- View this message in context: http://boost.2283326.n4.nabble.com/question-about-boost-pointer-tp4631069.ht... 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
Can I define: int a[100]; boost::shared_ptr<int> p(a); and get next int by using p? -- View this message in context: http://boost.2283326.n4.nabble.com/question-about-boost-pointer-tp4631069p46... Sent from the Boost - Users mailing list archive at Nabble.com.
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of young Sent: June-08-12 11:33 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] question about boost pointer
Can I define:
int a[100]; boost::shared_ptr<int> p(a);
and get next int by using p?
No. boost::shared_ptr is for objects created using operator new (and thus deleted using operator delete) while boost::shared_array is for objects created using operator new[] (and thus deleted using operator delete[]). use boost::shared_array instead, or for more flexibility, use std:;vector. HTH Ted
On Fri, Jun 08, 2012 at 08:33:11AM -0700, young wrote:
Can I define:
int a[100]; boost::shared_ptr<int> p(a);
and get next int by using p?
No, and that's wrong in several ways. A shared_ptr<T> conceptionally owns a single pointer of type T*, and is responsible for when destruction time comes, either invoke 'delete' or call the provided deleter function. If you want to refer to storage owned somewhere else, you would have to pass in a 'no-op' deleter in order to avoid multiple or incorrect destruction. If you want to own a sequence-of-T, like the ones allocated by 'new T[]', you would use a shared_array<T>. And yet again, the whole point of a shared_ptr and shared_array is shared ownership and responsibility among the instances for the pointed-to object or objects. If you start referring externally owned data, at best you're just giving users false hopes, and at worst causing accesses through dangling pointers or even multiple/incorrect destruction. -- Lars Viklund | zao@acc.umu.se
I am working on Visual C++ application. In my class, I define a data member as boost::shared_array and new it in one of the member function. If this member function is called many time. Then the array will be "newed" many times. Is that OK? I do want to new it many time because the size of array depends on the function parameters. -- View this message in context: http://boost.2283326.n4.nabble.com/question-about-boost-pointer-tp4631069p46... Sent from the Boost - Users mailing list archive at Nabble.com.
On Fri, Jun 8, 2012 at 2:18 PM, young
I am working on Visual C++ application.
In my class, I define a data member as boost::shared_array and new it in one of the member function. If this member function is called many time. Then the array will be "newed" many times. Is that OK?
I do want to new it many time because the size of array depends on the function parameters.
I think we need a small code example in order to understand what you want.
example: class MyClass { boost::shared_ptrboost::shared_array<char> pBuff; void getnextpacket(size) { pBuff = boost::shared_ptrboost::shared_array<char> (new boost::shared_array<char>(new char[size])); ... } void other_func() { // use pBuff here ... } } -- View this message in context: http://boost.2283326.n4.nabble.com/question-about-boost-pointer-tp4631069p46... Sent from the Boost - Users mailing list archive at Nabble.com.
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of young Sent: June-09-12 10:34 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] question about boost pointer
example:
class MyClass { boost::shared_ptrboost::shared_array<char> pBuff;
void getnextpacket(size) { pBuff = boost::shared_ptrboost::shared_array<char> (new boost::shared_array<char>(new char[size])); ... }
Why bother with boost::shared_ptr here? It adds nothing of value to the mix that I can see. Why not try something like: class MyClass { private: boost:shared_array<char> pBuff; public: void getNextPacket(size) { pBuff.reset(new char[size]); .... } ... } Cheers Ted
On Sat, Jun 9, 2012 at 8:16 AM, Ted Byers
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of young Sent: June-09-12 10:34 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] question about boost pointer
example:
class MyClass { boost::shared_ptrboost::shared_array<char> pBuff;
void getnextpacket(size) { pBuff = boost::shared_ptrboost::shared_array<char> (new boost::shared_array<char>(new char[size])); ... }
Why bother with boost::shared_ptr here? It adds nothing of value to the mix that I can see.
Why not try something like:
class MyClass { private: boost:shared_array<char> pBuff; public: void getNextPacket(size) { pBuff.reset(new char[size]); .... } ... }
Cheers
Ted
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
There's also the question of why you need a shared_ptr or shared_array here at all. Will the array be passed around where you can't easily scope the lifetime of the data? Brian
some other function also use pBuff, so it is class scop, not function scope -- View this message in context: http://boost.2283326.n4.nabble.com/question-about-boost-pointer-tp4631069p46... Sent from the Boost - Users mailing list archive at Nabble.com.
thank, Ted. -- View this message in context: http://boost.2283326.n4.nabble.com/question-about-boost-pointer-tp4631069p46... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (5)
-
Brian Budge
-
Diederick C. Niehorster
-
Lars Viklund
-
Ted Byers
-
young