简体   繁体   中英

C++ How should I return my byte array?

I've got a class named X (eg) and a public function toBytes() which returns some custom binary representation of the object.

My problem is: how should I return this byte array?

Currently, I have this:

uint8_t* X::toBytes()
{
    uint8_t* binData = new uint8_t[...];

    // initialize the byte array

    return binData;
}

The problem (or as problem considered by me as an inexperienced c++ programmer) here is that it's allocating the memory on the heap and should be freed at some point which I don't think is practical. Either the class X should free this memory in its destructor in some cumbersome way or the caller should free it. How is the caller supposed to know it is responsible for freeing the memory? It doesn't even know if it's heap memory, right?

Kinda stuck here :/

EDIT:

I just thought of a possible solution: let the caller supply the memory as in a pointer to a byte array and initialize that array. This would solve the problem, right?

I'd provide one generic solution and one convenience wrapper:

#include <iterator>
#include <vector>

template <typename OutIter>
void writeBytes(OutIter iter)
{
    for (...) { *iter = data(); ++iter; }
}

std::vector<uint8_t> toBytes()
{
    std::vector<uint8_t> result;
    writeBytes(std::back_inserter(result));
    return result;
}

The generic version allows the user to interface with any kind of container they choose, and the convenience "vector" version allows the user to write something like for (auto b : toBytes()) .

Assuming the size of the array is not known at compile time, use an std::vector and return it:

std::vector<uint8_t> toBytes()
{
  std::vector<uint8_t> binData = ....;
  return binData;
}

If the size is known at compile time, then you may prefer to use an std::array :

std::array<uint8_t, 42> toBytes()
{
  std::array<uint8_t, 42> binData = ....;
  return binData;
}

The std::vector method would most likely be the preferred choice. An alternative would be to return a std::unique_ptr :

std::unique_ptr<uint8_t[]> toBytes()
{
    std::unique_ptr<uint8_t[]> result(new uint8_t[size]);
    // ...
    return result;
}

This would only be useful if there was some limitation that prevented you from being able to utilize std::vector .

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