|
Boost Users : |
From: Nat Goodspeed (ngoodspeed_at_[hidden])
Date: 2006-10-31 11:03:04
> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of Stephen Torri
> Sent: Tuesday, October 31, 2006 9:34 AM
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] Safe way to allocate block of memory
>
> On Tue, 2006-10-31 at 06:49 -0500, Nat Goodspeed wrote:
> > [Nat] Have you tried using a std::vector<unsigned char>? You can get
a
> > pointer to the first byte in the vector by &myVector[0]. Then
> > std::vector takes care of all the rest for you -- including
allocation,
> > cleanup and the STL container support.
>
> // present_pos is an std::vector<boost::uint8_t>::iterator to the
> // where the reading will start.
> //
> // dest_addr_ptr is where to write in memory
> //
> // length is how many bytes to read.
> void read ( boost::uint8_t* dest_addr_ptr, boost::uint32_t const&
length )
> {
> if ( present_pos + length > data.end() )
> {
> // ERROR
> }
>
> memcpy ( dest_addr_ptr, *(*present_pos), length;
[Nat] memcpy ( dest_addr_ptr, &(*present_pos), length );
or even
// trust STL implementation to optimize appropriately
std::copy(present_pos, present_pos + length, dest_addr_ptr);
> }
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