简体   繁体   English

当指针作为方法的引用传递时,如何在 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

I am having issues storing values into a void* and successfully retrieving what I stored in initially.我在将值存储到 void* 并成功检索我最初存储的内容时遇到问题。 The below is my pseudo code/train of thought:以下是我的伪代码/思路:

Inside method 1 on a client客户端内部方法 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;
}

Inside the Stat method on the client, there's a protobuf via grpc that has the checksum for the file on the server:在客户端的 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
}

There's no reason to use a void* here at all.根本没有理由在这里使用void* C++ has a type system; C++ 有一个类型系统; you should use it.你应该使用它。

Instead of declaring your out parameter as a void* , declare it to be either a pointer or reference to the type you want to write.不要将 out 参数声明为void* ,而是将其声明为指向要写入的类型的指针或引用。 In this case that appears to be uint32_t :在这种情况下,似乎是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;
}

And then you can call it without doing any special gymnastics:然后你可以在不做任何特殊体操的情况下调用它:

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现场演示


If there's some reason you must use a void* and thus explicitly give up the protections offered by the type system then the pointer still has to point to something .如果出于某种原因您必须使用void*并因此明确放弃类型系统提供的保护,那么指针仍然必须指向something In the end the code will look very similar, just with an extra cast and significantly more opportunity to mess up and wander into the realm of undefined behavior:最后,代码看起来非常相似,只是有一个额外的演员和明显更多的机会来搞砸并徘徊在未定义行为的领域:

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;
}

Not much extra is needed when calling the function, since any pointer-to-object type can be implicitly converted to void* :调用函数时不需要太多额外的东西,因为任何指向对象的类型都可以隐式转换为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现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM