writetome
writetome

Reputation: 13

malloc C++ VS 2013

I'm trying to compile on Microsoft visual studio 2013 on C++ a program written for linux.

the statement

sdesc_t *ret = _malloc(sizeof(sdesc_t));

return: IntelliSense: a value of type "void *" cannot be used to initialize an entity of type "sdesc_t *"

any suggestion ?

Upvotes: 0

Views: 336

Answers (2)

user2845121
user2845121

Reputation: 113

Use type casting before set pointer value like

sdesc_t *ret = (sdesc_t *)_malloc(sizeof(sdesc_t));

As you written is void by default so it making error.

Upvotes: 0

Ajay
Ajay

Reputation: 18431

Use this:

sdesc_t *ret = (sdesc_t *)_malloc(sizeof(sdesc_t));

Or better use reinterpret_cast:

sdesc_t *ret = reinterpret_cast<sdesc_t *>(_malloc(sizeof(sdesc_t)));

And since you are using C++, you better use new:

sdesc_t *ret = new sdesc_t;

And since you are using latest C++ compiler that supports auto, you can:

auto ret = new sdesc_t;

And since STL has rich support for smart pointers, you can simply use them. For example:

auto ret = std::make_unique<sdesc_t>();

Upvotes: 7

Related Questions