0 c++ malloc visual-studio-2013
我正在尝试使用C++编写的用于Linux的Microsoft Visual Studio 2013编译.
该声明
sdesc_t *ret = _malloc(sizeof(sdesc_t));
Run Code Online (Sandbox Code Playgroud)
return:IntelliSense:类型为"void*"的值不能用于初始化"sdesc_t*"类型的实体
有什么建议吗?
用这个:
sdesc_t *ret = (sdesc_t *)_malloc(sizeof(sdesc_t));
Run Code Online (Sandbox Code Playgroud)
或者更好地使用reinterpret_cast:
sdesc_t *ret = reinterpret_cast<sdesc_t *>(_malloc(sizeof(sdesc_t)));
Run Code Online (Sandbox Code Playgroud)
由于您使用的是C++,因此最好使用new:
sdesc_t *ret = new sdesc_t;
Run Code Online (Sandbox Code Playgroud)
由于您使用的是支持的最新C++编译器auto,因此您可以:
auto ret = new sdesc_t;
Run Code Online (Sandbox Code Playgroud)
由于STL对智能指针有丰富的支持,您可以简单地使用它们.例如:
auto ret = std::make_unique<sdesc_t>();
Run Code Online (Sandbox Code Playgroud)