Advertisement
Hello, all ...
I want to use a smart pointer, preferably boosty, to encapsulate an array of unsigned chars that I need to allocate from the heap:
int main()
{
unsigned char *pBuf = new unsigned char[ 512 * 1024 ]; // icky, this isn't quite smart enough :-)
auto_ptr< unsigned char * > pBuf( new unsigned char[ 512 * 1024 ] ); // no work, auto_ptr dtor does not call delete[]
boost::scoped_array< unsigned char *> pBuf( new unsigned char[ 512 * 1024 ] ); // no work, boost expect an array of unsigned char pointers here, not an array of unsigned chars
// the thing I kinda need to do is ...
fread( pBuf, 1, lotsa_bytes, pFile );
}
... any hints? I'm not new to smart pointers, but I am kinda new to boost ...
Regards,
John
Falling You - exploring the beauty of voice and sound
www.fallingyou.com
Advertisement
Advertisement
-
Re: smart pointer for unsigned char[]?
Sun, June 18, 2006 - 6:30 PMstd::vector<unsigned char> -
-
Re: smart pointer for unsigned char[]?
Sun, June 18, 2006 - 6:51 PM
Scott,
Yes, I also consider a vector, but I need to mix this (for now) with old C-style APIs like fread / fwrite, which expect unsigned char pointers. Will std::vectors' ++ and [] operators "fool" fread / fwrite into thinking that they're dealing with an unsigned char *? I don't know -- I guess it's something to experiment with :-)
Regards,
John
Falling You - exploring the beauty of voice and sound
www.fallingyou.com
-
-
Re: smart pointer for unsigned char[]?
Sun, June 18, 2006 - 7:04 PMIt'll work fine, std::vector::begin() is your unsigned char pointer. -
-
Re: smart pointer for unsigned char[]?
Sun, June 18, 2006 - 7:13 PM&v[0] is the pointer to the beginning, as is &*v.begin(). v.begin() itself is not guaranteed to be a pointer, and it many implementations it is not.
For more information, read Effective STL.
-
-
-
-
Re: smart pointer for unsigned char[]?
Sat, July 15, 2006 - 12:14 PMJust out of curiosity why didnt you do this:
boost::scoped_array<unsigned char>pBuf(new unsigned char[512 *1024]);
You said you were needing an array of unsigned chars, not unsigned char *
and if scoped_array doesnt work (it has some limitations)
try shared_array (also in boost).
Perhaps I just didnt fully understand you implementation but it seems to me if you change your statement it will do what you want.
I have only been using boost a very short time, but I tell you I am sold on it, also be very careful with vectors under certain conditions the pointers, references and iterators can be invalidated without your realizing it. I used to be sold on vectors, now I find other containers more to my liking ;-) to each his own.