bob
2010-05-03 18:02:48 UTC
Hi,
I need to allocate a variable size struct using malloc() and then free it
using free(). Is there a way I could wrap a pointer returned from malloc()
in auto_ptr<>? I've used auto_ptr<> with objects of struct/class but only
when they were allocated using new() operator. AFAIK, it is generally not
safe to allocate with malloc() and then free with delete().
Example:
struct {
int x;
int y;
unsigned char data[1];
} structA;
structA* pa = (structA*) malloc(100);
I'd like to wrap pa pointer in auto_ptr<> so I do not have to worry about
freeing it when I leave a given block.
I think I could do the following:
unsigned char* p = new unsigned char[100];
structA* pa = (structA*) p;
_auto_ptr<unsigned char> ptrA(p);
[...]
Any thoughts?
Thanks.
I need to allocate a variable size struct using malloc() and then free it
using free(). Is there a way I could wrap a pointer returned from malloc()
in auto_ptr<>? I've used auto_ptr<> with objects of struct/class but only
when they were allocated using new() operator. AFAIK, it is generally not
safe to allocate with malloc() and then free with delete().
Example:
struct {
int x;
int y;
unsigned char data[1];
} structA;
structA* pa = (structA*) malloc(100);
I'd like to wrap pa pointer in auto_ptr<> so I do not have to worry about
freeing it when I leave a given block.
I think I could do the following:
unsigned char* p = new unsigned char[100];
structA* pa = (structA*) p;
_auto_ptr<unsigned char> ptrA(p);
[...]
Any thoughts?
Thanks.