gpt4 book ai didi

c++ - 当指针作为对方法的引用传递时,如何在 C++ 中使用 void* 来保存 uint32_t 的值

转载 作者:太空宇宙 更新时间:2023-11-04 15:55:26 34 4
gpt4 key购买 nike

我在将值存储到 void* 并成功检索我最初存储的内容时遇到问题。以下是我的伪代码/思路:

客户端内部方法1

StatusCode doSomething() {
string filename = "somefile.txt";
void* server_checksum;

//Stat signature (string &filename, void* file_status)
StatusCode fileStatus = Stat(filename, &server_checksum); //Passing the address of the pointer

//We received the fileStatus from Stat, I expect the value of server_checksum to match what the server sent
//However, this prints a completely different number, and I do not know how to ensure it holds the right value
cout << *((uint32_t *)serverCrc) << endl;

return StatusCode::OK;
}

在客户端的 Stat 方法中,有一个通过 grpc 的 protobuf,它具有服务器上文件的校验和:

StatusCode Stat(string &filename, void* file_status) {
//call the grpc method on the server (abstracted)
.
.
.
//Contains the checksum of the file on the server - this works fine
uint32_t s_crc = response.server_crc();

// I print it in both the server and the client to confirm it is the same value - this works fine
cout << s_crc << endl;

//In my understanding, here I am assigning the value of s_crc to the void * file status, which I passed the address for inside of method 1 - this works fine
file_status = (uint32_t *) &s_crc;

// I print file_status to make sure it still matches the value the server sent - this works fine
cout<<"file_status " << *((uint32_t *)file_status) << endl;

return StatusCode::OK; -> Continues inside method 1 above
}

最佳答案

根本没有理由在这里使用void*。 C++有一个类型系统;你应该使用它。

与其将输出参数声明为 void*,不如将其声明为指向您要写入的类型的指针或引用。在这种情况下,它似乎是 uint32_t:

StatusCode Stat(const std::string& filename, uint32_t& file_status) {
//call the grpc method on the server (abstracted)
// ...

//Contains the checksum of the file on the server - this works fine
file_status = response.server_crc();

return StatusCode::OK;
}

然后你可以在不做任何特殊体操的情况下调用它:

StatusCode doSomething() {
std::string filename = "somefile.txt";
uint32_t server_checksum;

StatusCode fileStatus = Stat(filename, server_checksum);

std::cout << server_checksum << std::endl;

return StatusCode::OK;
}

Live Demo


如果出于某些原因您必须使用void* 并因此显式放弃类型系统提供的保护,那么指针仍然必须指向东西。最后,代码看起来非常相似,只是多了一个强制转换,并且有更多机会搞砸并进入未定义行为的领域:

StatusCode Stat(const std::string& filename, void* file_status) {
//call the grpc method on the server (abstracted)
// ...

// cast to the appropriate pointer type
uint32_t* status_ptr = static_cast<uint32_t*>(file_status);

// now write the status to the object pointed to by the pointer passed to us
*status_ptr = response.server_crc();

return StatusCode::OK;
}

调用函数时不需要太多额外的东西,因为任何指向对象的指针类型都可以隐式转换为 void*:

StatusCode doSomething() {
std::string filename = "somefile.txt";
uint32_t server_checksum;

StatusCode fileStatus = Stat(filename, &server_checksum);

std::cout << server_checksum << std::endl;

return StatusCode::OK;
}

Live Demo

关于c++ - 当指针作为对方法的引用传递时,如何在 C++ 中使用 void* 来保存 uint32_t 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59042688/

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