smart pointer for unsigned char[]?

topic posted Sun, June 18, 2006 - 5:13 PM by 
Share/Save/Bookmark
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

posted by:
Advertisement
Advertisement
  • Re: smart pointer for unsigned char[]?

    Sat, July 15, 2006 - 12:14 PM
    Just 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.