Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost-users Digest, Vol 1945, Issue 2
From: tom tan (ttan_at_[hidden])
Date: 2009-03-25 06:04:21


From: Matthieu Brucher <matthieu.brucher_at_[hidden]>

>Perhaps simply:
>
>class base
>{
>public:
> base(size_t size):size(size), p_array(new unsigned char[size]){}
>private:
> size_t size;
> boost::shared_array<unsigned char> p_array;
>};
>
>And then:
>
>class derived:public base
>{
>public:
> derived(size_t array_size):base(array_size)
> {
> }
>};
>
>Do not declare size and p_array again, they will be different than the
>ones in the base class...
but in this case array_size is re-assigned after p_array was allocated, thus not
reflecting the size of the allocated buffer;
What I wished to achieve is:
1) the buffer is allocated inside the base class while
2) the size is designated inside the derived class.

>From: Christopher Currie <christopher_at_[hidden]>

>Also, shared_ptr won't work for arrays, you'll want to used
>"shared_array" instead.
That's right, I was thinking of shared_array while I was typing, but ...

>You might also think twice about this, and decide if having a shared
>copy of the buffer is really what you want, or if you're just using
>the smart pointer for memory management. If so, consider using
>"scoped_array" and defining a copy constructor with the appropriate
>semantics.

>If you go this route, you may just as well stick with 'vector'. It's
>less work, and probably far less overhead than you think.

I need some dynamically allocated space that can later be casted to C pointers,
 so that it can be passed to a Win32 API function that accepts C pointers only.
vectors are dynamic, but they don't cast to C arrays naturally.
In fact, I am wrapping that API in C++. is there a better way?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net