C ++:malloc:error:从“void *”到“uint8_t *”的无效转换 [英] C++: malloc : error: invalid conversion from ‘void*’ to ‘uint8_t*’

查看:2203
本文介绍了C ++:malloc:error:从“void *”到“uint8_t *”的无效转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这个问题:



从void *到uint8_t *的无效转换



执行此操作时:

  int numBytes; 
uint8_t * buffer;

buffer = malloc(numBytes); //错误这里,为什么?

还是我必须这样吗?

  buffer = malloc(numBytes); 

请说明...



解决方案

您不能从C ++中的 void * (不像C在这方面)。您可以:

  buffer = static_cast< uint8_t *>(malloc(numBytes)); 

但实际上你应该使用 new / delete 而不是 malloc / p>

I got this problem:

invalid conversion from ‘void*’ to ‘uint8_t*’

When doing this:

int             numBytes;
uint8_t         *buffer;

buffer=malloc(numBytes); //error here, why?

or must I have to put it like this?

buffer=malloc(numBytes); 

Please explain...

Thanks in advance.

解决方案

You cannot implicitly cast from void * in C++ (unlike C in this respect). You could do:

buffer = static_cast<uint8_t *>(malloc(numBytes));

but really, you should just be using new/delete instead of malloc/free!

这篇关于C ++:malloc:error:从“void *”到“uint8_t *”的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆