mercredi 2 mai 2018

C++ object containing an array of char using unique_ptr

I am looking on a way to use unique_ptr to allocate a structure that contains an array of char with a number of bytes that set dynamically to support different types of message.

Assuming:

struct MyMessage
{
    uint32_t      id;
    uint32_t      data_size;
    char          data[4];
};

How can I convert send_message() below to use a smart pointer?

void send_message(void* data, const size_t data_size)
{
    const auto message_size = sizeof(MyMessage) - 4 + data_size;
    const auto msg = reinterpret_cast<MyMessage*>(new char[message_size]);

    msg->id = 3;
    msg->data_size = data_size;
    memcpy(msg->data, data, data_size);

    // Sending the message
    // ...

    delete[] msg;
}

My attempt to use smart point using the code below does not compile:

const auto message_size = sizeof(MyMessage) - 4 + data_size;
const auto msg = std::unique_ptr<MyMessage*>(new char[message_size]);

Below a complete working example:

#include <iostream>
#include <iterator>
#include <memory>

using namespace std;

struct MyMessage
{
    uint32_t      id;
    uint32_t      data_size;
    char          data[4];
};

void send_message(void* data, const size_t data_size)
{
    const auto message_size = sizeof(MyMessage) - 4 + data_size;
    const auto msg = reinterpret_cast<MyMessage*>(new char[message_size]);
    if (msg == nullptr)
    {
        throw std::domain_error("Not enough memory to allocate space for the message to sent");
    }
    msg->id = 3;
    msg->data_size = data_size;
    memcpy(msg->data, data, data_size);

    // Sending the message
    // ...

    delete[] msg;
}

struct MyData
{
    int  page_id;
    char point_name[8];
};

void main()
{
    try
    {
        MyData data{};
        data.page_id = 7;
        strcpy_s(data.point_name, sizeof(data.point_name), "ab332");
        send_message(&data, sizeof(data));
    }
    catch (std::exception& e)
    {
        std::cout << "Error: " << e.what() << std::endl;
    }
}

Aucun commentaire:

Enregistrer un commentaire