Is it possible to use a C++ smart pointers togethe

2019-01-11 13:22发布

Some of my code still uses malloc instead of new. The reason is because I am afraid to use new because it throws exception, rather than returning NULL, which I can easily check for. Wrapping every call to new in a try{}catch(){} also doesn't look that good. Whereas when using malloc I can just do if (!new_mem) { /* handle error */ }.

Therefore I have a question. Can I use smart pointers together with malloc?

Something like:

SmartPointer<Type> smarty = malloc(sizeof(Type));

Something like this.

Is this possible?

Thanks, Boda Cydo.

9条回答
Rolldiameter
2楼-- · 2019-01-11 13:37

The best solution is to use new (std::nothrow) Type. This will act just like new Type, but will give null rather than throwing if it fails. This will be much easier than trying to make malloc behave like new.

If you really must use malloc, then remember to construct and destruct the object correctly:

void* memory = malloc(sizeof(Type));
Type* object = new (memory) Type;
object->~Type();
free(object); // or free(memory)

You can use this with a some smart pointers by giving it a custom deleter:

void malloc_deleter(Type* object)
{
    object->~Type();
    free(object);
}

if (void* memory = malloc(sizeof(Type)))
{
    Type* object = new (memory) Type;
    std::shared_ptr<Type> ptr(object, malloc_deleter);
    DoStuff(ptr);
}

But this would be much simpler using non-throwing new:

if (Type* object = new (std::nothrow) Type)
{        
    std::shared_ptr<Type> ptr(object);
    DoStuff(ptr);
}
查看更多
Root(大扎)
3楼-- · 2019-01-11 13:40

It depends on what the SmartPointer does on destruction. If you can specify free as a deallocator, that could work. For example, boost::shared_ptr allows you to specify a deleter.

I didn't pay enough attention to your reason for wanting this. I agree with the other answers that using the nothrow new is a much better idea.

查看更多
别忘想泡老子
4楼-- · 2019-01-11 13:45

It is possible to use malloc with smart pointers (you have to cast return value to target pointer type, though and provide custom deallocator). But better option is to use nothrow version of new operator.

http://www.cplusplus.com/reference/std/new/nothrow/

查看更多
在下西门庆
5楼-- · 2019-01-11 13:46

I have a question.

What happens if "Type" is a type whose constructor can throw? In that case, one still needs to handle exceptions in a try/catch block.

So is it a good idea to abandon exception based approach?

I would say that one can use the Abstract Factory/Factory Method design pattern and have all the 'new's in relatively lesser set of files/namespaces/classes, rather than these being scattered all around the place. That may also help in restricting the use of try/catch block to a relatively lesser code.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-11 13:50

You can use nothrow keyword with the new operator, which will return NULL rather that throw an exception. For details see link below: http://www.cplusplus.com/reference/std/new/nothrow/

查看更多
ら.Afraid
7楼-- · 2019-01-11 13:55

Use nothrow.

Nothrow constant

This constant value is used as an argument for operator new and operator new[] to indicate that these functions shall not throw an exception on failure, but return a null pointer instead.

char* p = new (nothrow) char [1048576];
if (p==NULL) cout << "Failed!\n";
else {
    cout << "Success!\n";
    delete[] p;
}
查看更多
登录 后发表回答