Re: [Boost-users] Boost-users Digest, Vol 1945, Issue 2
From: Matthieu Brucher <matthieu.brucher@gmail.com>
>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@currie.com>
>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?
but in this case array_size is re-assigned after p_array was allocated, thus not reflecting the size of the allocated buffer;
The order of initialization is following: 1) Base::size 2) Base::p_array So the array is allocated after the size is assigned.
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.
std::vector provides the guarantee that its elements are in contigous memory block, so &*vec.begin() is a ptr to c-like array.
On Wed, Mar 25, 2009 at 11:04 AM, tom tan <ttan@husky.ca> wrote:
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?
The first paragraph of the C++ standard which describes vector class guarantees for verctor<T> (not vector<bool>) to provide the pointer to a continuous block of Ts by using: &vec[0]. vector<bool> is specialization which stores bool values bitwise. Greetings, Ovanes
participants (3)
-
Igor R -
Ovanes Markarian -
tom tan