Replies: 3 comments 1 reply
-
What would be the expected output?
Il giorno sab 28 ott 2023 alle 01:41 f-peri ***@***.***> ha
scritto:
… I apologize in advance as this question is more a result of my lack of
experience with pointers rather than your implementation. I also understand
it's been some time since you've published this work. Thanks for putting
together a great routine. When I run this sketch:
#include <CircularBuffer.h>
CircularBuffer<char*, 4> buffer;
void printBuffer() {
if (buffer.isEmpty()) {
Serial.println("empty");
} else {
Serial.println("[");
for (decltype(buffer)::index_t i = 0; i < buffer.size(); i++) {
Serial.println(buffer[i]);
}
Serial.println("]");
Serial.print("(");
Serial.print(buffer.size());
Serial.print("/");
Serial.print(buffer.size() + buffer.available());
if (buffer.isFull()) {
Serial.print(" full");
}
Serial.println(")");
}
}
void setup() {
Serial.begin(115200);
while (!Serial)
delay(100);
char dataMessage[] = "1 is first in (oldest)";
buffer.unshift(dataMessage);
Serial.print("buffer is ");
printBuffer();
strcpy(dataMessage, "2 is second in");
//Serial.printf("dataMessage second = %s\n", dataMessage);
buffer.unshift(dataMessage);
Serial.print("buffer is ");
printBuffer();
strcpy(dataMessage, "3 is middle");
buffer.unshift(dataMessage);
Serial.print("buffer is ");
printBuffer();
strcpy(dataMessage, "4 is next to newest");
buffer.unshift(dataMessage);
Serial.print("buffer is ");
printBuffer();
strcpy(dataMessage, "5 is newest");
buffer.unshift(dataMessage);
Serial.print("buffer is ");
printBuffer();
}
void loop() {}
The result I get is:
buffer is [
1 is first in (oldest)
]
(1/4)
buffer is [
2 is second in
2 is second in
]
(2/4)
buffer is [
3 is middle
3 is middle
3 is middle
]
(3/4)
buffer is [
4 is next to newest
4 is next to newest
4 is next to newest
4 is next to newest
]
(4/4 full)
buffer is [
5 is newest
5 is newest
5 is newest
5 is newest
]
(4/4 full)
In my application the variable dataMessage is reused in loop(), but I've
simplified here to demonstrate my question. I have run the experiment with
multiple defined char arrays with success, however, when run as above, well
you see the results.
Thanks for your help,
Frank
—
Reply to this email directly, view it on GitHub
<#82>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABQWMMIXAGWCF2OD3ERX6TYBRBELAVCNFSM6AAAAAA6TP2CMCVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZVG44DKNZSGQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for your prompt reply. My desire is to reuse the variable dataMessage. If I rewrite the code to use separate char *, as such:
then the output is:
which is the output I expected. I am fairly certain I am missing something quite obvious and thank you for your help. |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can re-use the variable name, but you must assign a new memory space
each time and you also have to take care of releasing such memory space.
Have a look at the example where objects are stored in the buffer.
In practice you need to either use the new operator or the malloc (memory
allocate) operator: in your case malloc is the way to go.
The whole explanation is quite long winded, I suggest to get a proper
course about memory allocation, heap vs stack space and pointers, it’s not
something that can be explained on a forum thread.
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rlogiacco
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I apologize in advance as this question is more a result of my lack of experience with pointers rather than your implementation. I also understand it's been some time since you've published this work. Thanks for putting together a great routine. When I run this sketch:
The result I get is:
In my application the variable dataMessage is reused in loop(), but I've simplified here to demonstrate my question. I have run the experiment with multiple defined char arrays with success, however, when run as above, well you see the results.
Thanks for your help,
Frank
Beta Was this translation helpful? Give feedback.
All reactions