-1

I designed this smart buffer. I'd use std::vector but I don't want to copy things, I want to use a ready buffer and I want it to be self deleted when Buffer gets deleted.

struct Buffer
    {
    public:
        std::unique_ptr<uint8_t *> data;
        size_t len = 0;
        Buffer(CBuffer cBuffer)
        {
            data = std::make_unique<uint8_t *>(cBuffer.data);
            len = cBuffer.len;
        }
        Buffer(bool empty)
        {
            data = std::make_unique<uint8_t *>(nullptr);
            this->len = 0;
        }
        bool empty = false;
    };

The problem with this buffer is that I get errors like this:

error: cannot initialize a parameter of type 'unsigned char *const *' with an lvalue of type 'uint8_t *' (aka 'unsigned char *')
                auto b = return_something(raw_buffer, buffer.len);

in

uint8_t* raw_buffer = *buffer.data;
auto b = return_something(raw_buffer, buffer.len);

What is unsigned char *const *? And will this Buffer work and delete the managed buffer on destruction?

Guerlando OCs
  • 163
  • 8
  • 34
  • The error suggests that `return_something` does not take a parameter of type `uint8_t*`, which is what you are trying to pass to it. Show a [mcve], or at the very least the declaration of the function the error message actually complains about. You are showing a class that doesn't appear to have anything to do with the error message. – Igor Tandetnik Jul 20 '20 at 04:14
  • `Buffer` doesn't manage any buffer. It manages a single `uint8_t*` pointer (it's unclear why you feel the need to allocate that on the heap, but yes, it'll deallocate that pointer in its destructor). – Igor Tandetnik Jul 20 '20 at 04:15
  • @IgorTandetnik I want to manage an `uint8_t` buffer, not exactly a pointer to `uint8_t`, that's why I need it on the heap – Guerlando OCs Jul 20 '20 at 05:11

1 Answers1

1

Use std::vector as it clears memory by itself upon scope end. Use smart pointers, when standard containers are not possible.

Specific to your problem. The smart pointer has to be declared without *:

std::unique_ptr<X> (not X*)

iammilind
  • 62,239
  • 27
  • 150
  • 297
  • If I do that, then in the destruction wouldn't it delete only the value of the pointer? – Guerlando OCs Jul 20 '20 at 04:26
  • @Guerlando, upon the scope end, while destruction, the pointer [address] is deleted. Not the [indirected] pointer value. – iammilind Jul 20 '20 at 04:32
  • How should I construct this buffer? Compiler is complaining about me being not able to initialize `std::unique_ptr(buffer.data)` where `buffer.data` is of type `uint8_t*` – Guerlando OCs Jul 20 '20 at 04:36
  • @GuerlandoOCs, from your above query, I realise that what are you trying to achieve. You may refer the link duplicate post for the same. In a nutshell, if you don't want to use `std::vector` or `std::string` etc, then use `std::unique_ptr`. – iammilind Jul 20 '20 at 05:15
  • Ok, I tried with `std::make_unique` but it didn't work. Then I simply did `data = std::unique_ptr` and it worked. However I don't know how to get the raw pointer `uint8_t*` for me to write inside. On the suggested answer there's nothing about this – Guerlando OCs Jul 20 '20 at 05:27
  • @GuerlandoOCs, you may use the [`unique_ptr::get()`](http://www.cplusplus.com/reference/memory/unique_ptr/get/) method which provides the underlying pointer. – iammilind Jul 20 '20 at 05:46