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

How to use a void* in C++ to hold the value of an uint32_t when the pointer is passed as a reference to a method

我在将值存储到 void* 并成功检索最初存储的内容时遇到问题。下面是我伪code/train的想法:

在客户端的内部方法 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