-
Hi I am reading a char* (like "See cat play") into RAM and I want to save it into your CircB but the readme does not explain how to. Can you tell me please? Of course, when circb gets full, I want it to free the RAM when it dumps the oldest entry. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 16 comments
-
Considering this question it's probably better fit for a StackExchange entry, a quick answer is probably to read one char at a time and push each char into a buffer declared as operating on |
Beta Was this translation helpful? Give feedback.
-
Thank you but I need to be able to retrieve a string at a time can your
library work with strings
|
Beta Was this translation helpful? Give feedback.
-
The library can work with strings, with each string occupying a slot of the buffer: This buffer will store a maximum of 32 strings, each one represented as an array of chars: no limit is imposed on the length of the strings (either combined or not), with the wrap around occurring when you try to push the 33th string. If you want to operate on the String object, the library can operate on objects and structures as well (see examples). I don't know what your goal is, but my gut feeling is you are on the wrong track here. |
Beta Was this translation helpful? Give feedback.
-
@geetee24 I'm not sure either what you're trying to do. If you're planning on storing ordered strings in PROGMEM have a look C++ containers like std::queue, or vector and list. I use Roberto's code because its small and super efficient, but if speed and memory is not an issue for you also look at using containers. |
Beta Was this translation helpful? Give feedback.
-
Let's say I am reading a series of strings coming in from an IOT device and
I simply want to save each of those strings into a circular buffer for a
later inspection
|
Beta Was this translation helpful? Give feedback.
-
The simplest way to achieve that is by storing Please note the memory consumption is unknown at compile time. in both cases: longer the char sequences, more the memory required to store them. Have I forgot something @proddy ? |
Beta Was this translation helpful? Give feedback.
-
That'll work if you want to be safe and preallocate some RAM. If you don't care use a vector/list/queue which dynamically allocates memory when a new record is added. It is costly though. Also I would avoid using Strings at all costs 'cos their plain ole nasty. If the idea is to read in strings say from UART stream or MQTT messages then I would create a struct as you'll probably want to store additional info like string length and timestamps. |
Beta Was this translation helpful? Give feedback.
-
I agree with you. Never use "Strings". just want to know if the Cicr
lib can handle a series of "char* and FREE them up when the CircB is full ?
…On Mon, Feb 11, 2019 at 11:00 AM Proddy ***@***.***> wrote:
That'll work if you want to be safe and preallocate some RAM. If you don't
care use a vector/list/queue which dynamically allocates memory when a new
record is added. It is costly though. Also I would avoid using Strings at
all costs 'cos their plain ole nasty. If the idea is to read in strings say
from UART stream or MQTT messages then I would create a struct as you'll
probably want to store additional info like string length and timestamps.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/rlogiacco/CircularBuffer/issues/19#issuecomment-462451156>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGXqT98li8FwtLpMVgl1-XYaSGCJgneRks5vMb3egaJpZM4azI4z>
.
|
Beta Was this translation helpful? Give feedback.
-
not sure what you mean about 'free'. The allocated memory is already reserved so there's nothing to free up if you're referring to memory. It's a circular buffer so it'll just go round and round. As long as you correctly null-terminate the char* you won't be picking up stray data from unused memory. |
Beta Was this translation helpful? Give feedback.
-
the circ buffer had a feature that if you add more elements than it can
hold, it will remove the oldest element.
…On Mon, Feb 11, 2019 at 11:29 AM Proddy ***@***.***> wrote:
not sure what you mean about 'free'. The allocated memory is already
reserved so there's nothing to free up if you're referring to memory. It's
a circular buffer so it'll just go round and round. As long as you
correctly null-terminate the char* you won't be picking up stray data from
unused memory.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/rlogiacco/CircularBuffer/issues/19#issuecomment-462461544>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGXqT3lZ37ThF29M281K15rJZ71a49fHks5vMcScgaJpZM4azI4z>
.
|
Beta Was this translation helpful? Give feedback.
-
that's correct, the entry is overwritten |
Beta Was this translation helpful? Give feedback.
-
the question I want to know, is does the library FREE (release up) the
memory that the char* used?
…On Mon, Feb 11, 2019 at 11:56 AM Proddy ***@***.***> wrote:
that's correct, the entry is overwritten
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/rlogiacco/CircularBuffer/issues/19#issuecomment-462471313>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGXqT2tmGGMWaZ3GkFzmwX0RjlFMV5amks5vMcr7gaJpZM4azI4z>
.
|
Beta Was this translation helpful? Give feedback.
-
no it doesn't! This is what Roberto explained earlier. When you initialize the buffer the compiler will allocate the memory. That what makes it zippy fast |
Beta Was this translation helpful? Give feedback.
-
The Now the question is, how would you dispose of such a sequence of characters? If you were reading some stuff from the serial input, would you call a |
Beta Was this translation helpful? Give feedback.
-
But I thought when the buffer circular was full search of a buffer its self
throughout the oldest entry and therefore I ask does the library or how
does the library free app that memory
|
Beta Was this translation helpful? Give feedback.
-
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 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.unshift(next_item); |
Beta Was this translation helpful? Give feedback.
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
andunshift
: