可变长度结构 [英] Variable length structures
问题描述
OMX提供了一个结构,定义如下
OMX provides a struct with following definition
/* Parameter specifying the content URI to use. */
typedef struct OMX_PARAM_CONTENTURITYPE
{
OMX_U32 nSize;
/**< size of the structure in bytes */
OMX_VERSIONTYPE nVersion; /**< OMX specification version information */
OMX_U8 contentURI[1]; /**< The URI name*/
}OMX_PARAM_CONTENTURITYPE;
OMX_IndexParamContentURI,
/**< The URI that identifies the target content. Data type is OMX_PARAM_CONTENTURITYPE. */
我有一个恒定的字符数组来设置。
I have got a constant char array to set.
char* filename = "/test.bmp";
据我的理解,我需要以某种方式的存储器复制文件名设置为struct.contentURI,然后相应地更新struct.size。我该怎么办呢?
As far as I understood I need somehow to set memcopy filename to struct.contentURI and then to update the struct.size accordingly. How can I do it?
最好的问候
推荐答案
首先,你需要分配足够的内存来包含固定大小部分和文件名:
First you need to allocate enough memory to contain the fixed-size parts and the filename:
size_t uri_size = strlen(filename) + 1;
size_t param_size = sizeof(OMX_PARAM_CONTENTURITYPE) + uri_size - 1;
OMX_PARAM_CONTENTURITYPE * param = malloc(param_size);
加1,以包括终止字符,并减去1,因为结构已经包含一个字节的阵列。
adding 1 to include the termination character, and subtracting 1 because the structure already contains an array of one byte.
在C ++中,你需要一个演员,你应该使用智能指针或异常安全的载体:
In C++, you'll need a cast, and you should use a smart pointer or a vector for exception safety:
std::vector<char> memory(param_size);
OMX_PARAM_CONTENTURITYPE * param =
reinterpret_cast<OMX_PARAM_CONTENTURITYPE *>(&memory[0]);
然后就可以在字段中填入:
Then you can fill in the fields:
param->nSize = param_size;
param->nVersion = whatever;
memcpy(param->contentURI, filename, uri_size);
和不要忘了免费(参数)
一旦你完成了它。
and don't forget to free(param)
once you've finished with it.
这篇关于可变长度结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!