Skip to content

Can you explain how to add strings (char *) to Circbuffer #19

Answered by rlogiacco
geetee24 asked this question in Q&A
Discussion options

You must be logged in to vote

You already got the answer you were looking for: as the buffer does not allocate memory it is not allowed to deallocate it, as simple as that. You, the user, are in charge of allocating/deallocating memory for objects on the heap.

How? Well, you check if you have any available space: if you don't, then pop and deallocate before pushing another one: the functions you are looking for are pop, available and unshift:

your_type next_item = new your_type; // allocate memory either with new or malloc
if (available() == 0) {
  // buffer is full, dispose before adding
  your_type t = buffer.pop();
  delete t; // dispose of your heap accordingly to your allocation method: delete or free
}
buffer.un…

Replies: 16 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by rlogiacco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #19 on December 12, 2020 14:22.