Home > Net >  Do I have to free the malloc memory in std::queue?
Do I have to free the malloc memory in std::queue?

Time:04-22

I have a doubt. Let's say I have a buffer allocated with malloc as the following:

uint16_t buffer_length = 200; // !size is dynamic!, never static
uint8_t* buffer = (uint8_t*) malloc(200*sizeof(uint8_t));
// buffer then is populated somehow from 0 to 199

Now I have a std::queue which I need to FIFO several allocated buffers as the following:

std::vector<uint8_t> buffer_vector(buffer, buffer   buffer_length);
std::queue<std::vector<uint8_t>> fifo_queue;
fifo_queue.push(buffer_vector);

Considering that I've decided to move from uint8_t* to std::vector because I can store more buffer information (length) in a single element. Now I want to take out the items from the queue:

std::vector<uint8_t> taken_item = fifo_queue.front(); // reads the FIFO item
fifo_queue.pop(); // removes from FIFO
uint8_t* taken_item_buffer_ptr = taken_item.data(); // takes the buffer ptr
uint16_t taken_item_buffer_length = taken_item.capacity(); // takes the size

Now I can handle the returned buffer from the FIFO, the question is, do I have to free the returned pointer? For example as the following:

free(taken_item_buffer_ptr);

Is this a good approach to store inside a FIFO dynamically-generated buffers? Thank you.

CodePudding user response:

std::vector<uint8_t> buffer_vector(buffer, buffer buffer_length); This will copy the data in buffer into a new memory region, "managed" by buffer_vector. Additionally fifo_queue.push(buffer_vector); will copy the data again. A second copy of the data is managed by fifo_queue.

So actually your question does not really make sense. You have to free buffer to avoid memory leaks. You don't have to free buffer_vector or fifo_queue. You also don't have to free taken_item_buffer_ptr. taken_item is a object of std::vector type and will call its destructor when it goes out of scope.

CodePudding user response:

do I have to free the returned pointer?

You need to, and are allowed to free only what you have allocated with malloc (or related C standard functions). You did not use those functions to get taken_item_buffer_ptr, so you aren't allowed to free.

std::vector maintains its own buffer. When the vector object is destroyed, it will free the memory in its destructor.

You are required to free buffer since you allocated that with malloc. But you shouldn't have allocated it in the first place. You should create a vector in the first place, and populate that directly.

Is this a good approach to store inside a FIFO dynamically-generated buffers? Thank you.

What I would change:

fifo_queue.emplace_back(buffer_length);
//  fifo_queue.back() then is populated somehow from 0 to 199

//...
std::vector<uint8_t> taken_item = std::move(fifo_queue.front());
fifo_queue.pop();

to avoid unnecessary copying the buffer.

uint16_t taken_item_buffer_length = taken_item.capacity(); // takes the size

This is wrong. Capacity is not the size. You can get the size using the member function size.

  • Related