gpt4 book ai didi

c++ - 将对象指针转换为双空指针(指向空指针的指针)

转载 作者:行者123 更新时间:2023-12-04 07:43:39 25 4
gpt4 key购买 nike

我在库中有一个类方法(无法更改它),它采用双空指针作为其参数之一。声明如下:

bool Queue::pop(void **packet, int &size, unsigned short &sn)
在我的应用程序代码中,我想将此函数传递一个指向另一种对象的指针,在本例中为 GstBuffer指针类型。如果我将指针转换为 (void**),我似乎可以在没有任何编译器错误的情况下执行此操作。如以下代码段所示,但我怀疑这是否会导致正确的行为。像这样转换指针是否有效?
guint16 sn;
int size;
GstBuffer *buf;
Queue *Q = ... // create a Queue instance
Q->pop((void **)&buf, size, sn); // is this conversion valid?
size = gst_buffer_get_size(buf);

最佳答案

出于所有意图和目的,void指针可以保存任何对象(数据类型)的地址,它可以指向任何对象,并且可以被类型转换为任何对象,你的代码是有效的,我只会使用更惯用的类型转换:

Q->pop(reinterpret_cast<void**>(&buf), size, sn);
§7.3.12 指针转换 [conv.ptr]
  1. A prvalue of type “pointer to cv T”, where T is an object type, can be converted to a prvalue of type “pointer to cv void”. The pointer value (6.8.3) is unchanged by this conversion.

Example:
void example(void **packet){
std::cout << *packet << "\n"; // pointer value
std::cout << packet << "\n"; // pointer address
std::cout << **reinterpret_cast<int**>(packet); // value
}

int main()
{
int* x = new int(20);
std::cout << x << "\n"; // pointer value
std::cout << &x << "\n"; // pointer address
example(reinterpret_cast<void**>(&x));
delete x;
}
输出:
0xb83eb0
0x7ffc181ab2c8
0xb83eb0
0x7ffc181ab2c8
20
甚至只需要显式转换,因为它是指向指针的指针,否则转换将是隐式的,不需要转换。

关于c++ - 将对象指针转换为双空指针(指向空指针的指针),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67313767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com