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

CjR*_*bin 0 c++ pointers protocol-buffers grpc

我在将值存储到 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;
}
Run Code Online (Sandbox Code Playgroud)

在客户端的 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
}
Run Code Online (Sandbox Code Playgroud)

Mil*_*nek 6

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

与其将 out 参数void*声明为 a ,不如将其声明为指向要写入的类型的指针或引用。在这种情况下,似乎是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;
}
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

现场演示


如果出于某种原因您必须使用 avoid*并因此明确放弃类型系统提供的保护,那么指针仍然必须指向something。最后,代码看起来非常相似,只是有一个额外的演员和明显更多的机会来搞砸并徘徊在未定义行为的领域:

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;
}
Run Code Online (Sandbox Code Playgroud)

调用函数时不需要太多额外的东西,因为任何指向对象的类型都可以隐式转换为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;
}
Run Code Online (Sandbox Code Playgroud)

现场演示