我想设置unsigned char*
数据到std::shared_ptr<uint8_t>
并将该指针传递给一个函数,并且在该函数中我想要得到unsigned char*
数据来自std::shared_ptr<uint8_t>
.
实际上我想读取图像数据如下。
FILE *fp=fopen("/data/FR/AjayPatil.jpg","rb");
if(fp)
{
fseek(fp, 0, SEEK_END);
data_size=ftell(fp);
fseek(fp, 0, SEEK_SET);
jdata=( unsigned char *)malloc(data_size+1);
if (!jdata)
{
fclose(fp);
return;
}
fread(jdata, data_size, 1, fp);
fclose(fp);
}
现在我有数据jdata
如你所见。
那么我怎样才能将这些数据复制到std::shared_ptr<uint8_t>
?
我的图像结构如下。
typedef struct Image
{
uint16_t width;
uint16_t height;
uint8_t depth;
std::shared_ptr<uint8_t> data;
}
另外,在成功复制数据后,以下操作是恢复数据的正确方法吗?
unsigned char *imagedata=data.get(); // here data means std::shared_ptr<uint8_t> data;
The image
的数据成员data
下面的代码是一个空的std::shared_ptr<uint8_t>
(没有要管理的对象):
Image image;
设置管理对象
通过致电std::shared_ptr
's reset()
您可以将托管对象设置为成员函数jdata
指着:
image.data.reset(jdata, free);
请注意,您必须提供free
as the deleter,因为数据指向jdata
是通过以下方式分配的malloc
:
jdata=(unsigned char *)malloc(data_size+1);
不提供free
as the deleter会导致delete
(即:默认删除器)被用于data
的破坏。
获取管理对象
为了获取指向托管对象的指针,您可以调用std::shared_ptr
's get()
会员功能:
auto imagedata=data.get();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)