可变长度结构

OMX提供了具有以下定义的结构

/* 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. */ 

我有一个常量的char数组来设置。

 char* filename = "/test.bmp"; 

据我所知,我需要以某种方式将memcopy文件名设置为struct.contentURI,然后相应地更新struct.size。 我该怎么做?

最好的祝福

首先,您需要分配足够的内存来包含固定大小的部分和文件名:

 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,因为结构已包含一个字节的数组。

在C ++中,您需要一个强制转换,并且您应该使用智能指针或向量来实现exception安全:

 std::vector memory(param_size); OMX_PARAM_CONTENTURITYPE * param = reinterpret_cast(&memory[0]); 

然后你可以填写以下字段:

 param->nSize = param_size; param->nVersion = whatever; memcpy(param->contentURI, filename, uri_size); 

一旦你完成它,不要忘记free(param)