# std::auto_ptr (smart pointer) with custom structure size.

This topic is 4520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have the following code
INTERNET_CACHE_ENTRY_INFO* m_Info;
m_Info = (INTERNET_CACHE_ENTRY_INFO*) new char[4096];
and I want to wrap it into a smart pointer. The problem is, when I declare the variable as
auto_ptr<INTERNET_CACHE_ENTRY_INFO> m_Info;
I can not change the ammount of memory allocated. Basically, I don't know if I'm approaching this correctly. I need to wrap the above code into a smart pointer. Please help :) (first post by the way)

##### Share on other sites
first gut reaction: use std::vector. then you don't need to wrap it in an auto_ptr. but you haven't given enough info to give a better answer.

guess:
class MyClass {   private:      std::vector<char> m_Info;      ...//but you may be able to get away with:class MyClass {   private:      std::string m_Info;      ...

##### Share on other sites
Remember that whenever you're using array-new, you need to use delete [] to properly destroy that array. However, auto_ptr's destructor invokes only non-array delete. Consequently, the array is not properly destroyed and the program's behavior is undefined. To summarize, auto_ptr should only hold a pointer to a single object that was allocated by new.

#include

int main (int argc, char * const argv[]) {

std::auto_ptr p1(new std::string("hello World!"));
std::cout <

##### Share on other sites
err...It cut off what I wrote???

references:
http://gethelp.devx.com/techtips/cpp_pro/10min/10min1199.asp
http://www.gotw.ca/publications/using_auto_ptr_effectively.htm

##### Share on other sites
Quote:
 Original post by Anonymous Postererr...It cut off what I wrote???

You will want to use &lt; for "<" and &gt; for ">" since AP replies are stripped of html tags. Since <> are used in html tags, those are removed, thus requiring the html entitiy codes instead.

##### Share on other sites
Warning: Opinions probably included.

You have half a bazillion problems. Well, a lot, anyways.

1) INTERNET_CACHE_... is likely not fully defined at that point, so std::auto_ptr dosn't know how to delete it.

2) You're really allocating char[], not INTERNET_CACHE_..., so even if it is fully defined, it probably won't correctly delete the array.

3) std::auto_ptr uses delete. An array allocated with new char[...] must be cleaned up with delete[]. Failure to do so will at best screw you over, at worst give you a very hard to find bug.

For arrays which don't need transfering ownership (you don't need it) I'd recommend boost::scoped_array - it has a counterpart in boost::scoped_ptr, which also prevents you from accidentally transfering ownership [boost website].

Assuming INTERNET_CACHE_... isn't a typedef for (char *) or (char []) or similar (you should assume it's not, since they went to the bother of giving it a typedef), here is a dirty method of dealing with all this working correctly:

boost::scoped_array< char > m_Info_data;
m_Info_data.reset( new char[ 4096 ] );

INTERNET_CACHE_ENTRY_INFO* m_Info = reinterpret_cast< INTERNET_CACHE_ENTRY_INFO * >( m_Info_data.get() ); //possibly wrap up into a simple utility function instead of having it a member... e.g.:

private: INTERNET_CACHE_ENTRY_INFO* Info() { return reinterpret_cast< INTERNET_CACHE_ENTRY_INFO * >( m_Info_data.get() ); }

4) INTERNET_CACHE_ENTRY_INFO has zero encapsulation of allocation, and should be dealt with using a wrapper class that encapsulates both this and deallocation. Look up RAII (Resource Aquisition Is Initialization) for the concept which makes me say this and to expand on what I'm talking about.

Warning: Paragraph contains opinions.
Also, well formed code interfacing with well formed code shouldn't need reinterpret_cast - and well formed code needing to interface in such a manner should encapsulate these casts as much as possible. Why? Two words: reduced mantinence. I base this on my opinion that just because it may have been well formed in the bygone days of C and it was easy to out-optimize your compiler dosn't mean it's well formed today. Times change.

-Mike

##### Share on other sites
Mike, thanks... boost::scoped_array is what I used.

It's probobly easier to stick with new[]/delete[] then go through all this extra stuff.