Discussing several options, starting from the good old C-style void* pointer.
An interesting question you may ask in C++ is: “How would you declare a function that takes a blob of memory as input?”
For example, think of a function that hashes some input data (using SHA-256, or whatever hash algorithm), or a function that takes some binary data and writes that to disk.
Coming from my C background, an option that came to mind would certainly be:
void DoSomething(const void* p, size_t numBytes)
You simply pass a const void* pointer to the beginning of the input memory block, and the total size of the memory block, expressed in bytes.
Then, some C++ programmer could start complaining: “Hey, why do you use the unsafe old C-style void* pointer? Use some safe explicit type like uint8_t, which clearly represents an 8-bit byte!”.
So, they propose to “step up” to the following prototype:
void DoSomething(const uint8_t* p, size_t numBytes)
Now, suppose that you want to pass to this function a custom structure, like this:
... continue reading