Tsa*_*res 1 c++ casting void void-pointers
使用以下代码片段:
int n = 11;
int* c = &n;
void** v = &c;
Run Code Online (Sandbox Code Playgroud)
我在 Visual Studio 中收到以下错误:
type 的值
int**不能用于初始化 type 的实体void **。
这工作正常:
int n = 11;
int* c = &n;
void* v = c;
Run Code Online (Sandbox Code Playgroud)
但这个代码片段是针对某人的库中的一个更大的问题。
我将变量转换为 时做错了什么 void**?
使用caen 数字化仪库尝试从外围设备收集数据的方式具有以下原型:
/******************************************************************************
* X742_DecodeEvent(char *evtPtr, void **Evt)
* Decodes a specified event stored in the acquisition buffer writing data in Evt memory
* Once used the Evt memory MUST be deallocated by the caller!
*
* [IN] EventPtr : pointer to the requested event in the acquisition buffer (MUST BE NULL)
* [OUT] Evt : event structure with the requested event data
* : return 0 = Success;
******************************************************************************/
int32_t X742_DecodeEvent(char *evtPtr, void **Evt);
Run Code Online (Sandbox Code Playgroud)
这是实现:
int32_t X742_DecodeEvent(char *evtPtr, void **Evt) {
CAEN_DGTZ_X742_EVENT_t *Event;
uint32_t *buffer;
char chanMask;
uint32_t j,g,size;
uint32_t *pbuffer;
uint32_t eventSize;
int evtSize,h;
evtSize = *(long *)evtPtr & 0x0FFFFFFF;
chanMask = *(long *)(evtPtr+4) & 0x0000000F;
evtPtr += EVENT_HEADER_SIZE;
buffer = (uint32_t *) evtPtr;
pbuffer = (uint32_t *) evtPtr;
eventSize = (evtSize * 4) - EVENT_HEADER_SIZE;
if (eventSize == 0) return -1;
Event = (CAEN_DGTZ_X742_EVENT_t *) malloc(sizeof(CAEN_DGTZ_X742_EVENT_t));
if (Event == NULL) return -1;
memset( Event, 0, sizeof(CAEN_DGTZ_X742_EVENT_t));
for (g=0; g<X742_MAX_GROUPS; g++) {
if ((chanMask >> g) & 0x1) {
for (j=0; j<MAX_X742_CHANNEL_SIZE; j++) {
Event->DataGroup[g].DataChannel[j]= malloc(X742_FIXED_SIZE * sizeof (float));
if (Event->DataGroup[g].DataChannel[j] == NULL) {
for (h=j-1;h>-1;h++) free(Event->DataGroup[g].DataChannel[h]);
return -1;
}
}
size=V1742UnpackEventGroup(g,pbuffer,&(Event->DataGroup[g]));
pbuffer+=size;
Event->GrPresent[g] = 1;
}
else {
Event->GrPresent[g] = 0;
for (j=0; j<MAX_X742_CHANNEL_SIZE; j++) {
Event->DataGroup[g].DataChannel[j] = NULL;
}
}
}
*Evt = Event;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我通过以下方式使用它:
CAEN_DGTZ_X742_EVENT_t* Evt = NULL; // Creating my event pointer
//Doing some config of the device
X742_DecodeEvent(evtptr, &Evt); //Decode the event data for me to read (Throws error)
Run Code Online (Sandbox Code Playgroud)
希望这能提供一些背景信息。
void**表示指向void*对象的指针。但是该代码中没有void*可以指向的对象!void**并不意味着“指向任何类型指针的指针”,因此请避免这样使用它。如果您有一个指向可能是 a int*、可能是 adouble*或等等的指针,则void*这是比 更好的类型void**。更好的是模板 或std::variant或std::any。
但是,如果您必须使用一个用于void**表示“指向编译时未知类型的指针”或类似内容的库,您可能需要创建一个void*要使用的指针,或者可能需要添加强制转换绕过编译器不喜欢这种转换的事实(有充分的理由)。问题是,至少有两种合理的方法可以做到这一点!(他们最终会在许多常见的计算机架构上做完全相同的事情,但这并不能保证。)
// LibraryFunc1 takes a void** argument that somehow means an int* pointer.
// But which call is correct?
int* data_in = generate_data();
LibraryFunc1(reinterpret_cast<void**>(&data_in)); // ?
void* p1 = data_in;
LibraryFunc1(&p1); // ?
// LibraryFunc2 returns a void** argument that somehow means an int* pointer.
void** p2 = LibraryFunc2();
int* data_out_1 = static_cast<int*>(*p2); // ?
int* data_out_2 = *reinterpret_cast<int**>(p2); // ?
Run Code Online (Sandbox Code Playgroud)
不幸的是,根据所示的函数定义,安全用法是:
void* tmpEvt;
X742_DecodeEvent(evtptr, &tmpEvt);
auto* Evt = static_cast<CAEN_DGTZ_X742_EVENT_t*>(tmpEvt);
Run Code Online (Sandbox Code Playgroud)
因为库函数假设*Evt = Event;它*Evt实际上是一个void*它可以修改的对象。通常可以做更简单的事情:
CAEN_DGTZ_X742_EVENT_t* Evt = NULL;
X742_DecodeEvent(evtptr, reinterpret_cast<void**>(&Evt));
Run Code Online (Sandbox Code Playgroud)
但这是 C++ 标准未定义的行为,并且可能在某些体系结构上做错误的事情。
您可以通过将其包装在函数中来使正确的方法更容易:
inline CAEN_DGTZ_X742_EVENT_t* Get_X742_DecodeEvent(char* evtPtr)
{
void* tmpEvt;
X742_DecodeEvent(evtPtr, &tmpEvt);
return static_cast<CAEN_DGTZ_X742_EVENT_t*>(tmpEvt);
}
Run Code Online (Sandbox Code Playgroud)