简体   繁体   中英

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. The below is my pseudo code/train of thought:

Inside method 1 on a client

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:

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. C++ has a type system; 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. In this case that appears to be 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 . 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* :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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