Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing a single data fills the buffer #58

Closed
pkrssy opened this issue Dec 5, 2021 · 4 comments
Closed

Writing a single data fills the buffer #58

pkrssy opened this issue Dec 5, 2021 · 4 comments
Labels

Comments

@pkrssy
Copy link

pkrssy commented Dec 5, 2021

v1.3.3
If a single data is written into the newly created buffer, buffer.size() reports one but all the other elements of the buffer is filled with the data.

#include <CircularBuffer.h>
CircularBuffer<long, 6> buffer;
uint16_t data1 = 1111;

void setup() 
{

  Serial.begin(9600);
  Serial.print("Buffer size before writing: ");
  Serial.println(buffer.size());
  buffer.push(data1);
  Serial.print("Buffer size after writing: ");
  Serial.println(buffer.size());
  Serial.print("Elements: ");
  Serial.print(buffer[0]);
  Serial.print(", ");
  Serial.print(buffer[1]);
  Serial.print(", ");
  Serial.print(buffer[2]);
  Serial.print(", ");
  Serial.print(buffer[3]);
  Serial.print(", ");
  Serial.print(buffer[4]);
  Serial.print(", ");
  Serial.println(buffer[5]);
   
}

void loop() {
  
}

Result:

09:28:44.723 -> Buffer size before writing: 0
09:28:44.856 -> Buffer size after writing: 1
09:28:44.889 -> Elements: 1111, 1111, 1111, 1111, 1111, 1111

The behavior can not be experienced in v1.3.0

@rlogiacco
Copy link
Owner

rlogiacco commented Dec 5, 2021

Any access beyond the current buffer bounds is a violation of the contract and assuming anything about the buffer internal state is a usage error.
If the values returned for the populated buffer positions is correct then the buffer operates as intended.

In other words, accessing positions beyond 0 is an error, you are making assumptions on how the buffer operates on its internals.

It wasn't arising in the previous version because the buffer wasn't protecting its internal state properly: now the buffer does that better and it only allows you to access populated positions. If you have only one element it doesn't matter what's the index you provide, you will always get that single item.
Push in another one and you'll see the two values alternating...

@rlogiacco rlogiacco added waiting-feedback Waiting for feedback from the submitter wontfix labels Dec 6, 2021
@rlogiacco
Copy link
Owner

Another way to describe the behaviour, if you want to print the buffer contents you should only print the items between position 0 and position buffer.size() - 1: in your case between 0 and 0 after the first insertion....

See the two for-loops examples at https://github.com/rlogiacco/CircularBuffer#automatic-optimization and the code at

void printBuffer() {
if (buffer.isEmpty()) {
Serial.println("empty");
} else {
Serial.print("[");
for (decltype(buffer)::index_t i = 0; i < buffer.size() - 1; i++) {
Serial.print(buffer[i]);
Serial.print(",");
}
Serial.print(buffer[buffer.size() - 1]);
Serial.print("] (");
Serial.print(buffer.size());
Serial.print("/");
Serial.print(buffer.size() + buffer.available());
if (buffer.isFull()) {
Serial.print(" full");
}
Serial.println(")");
}
}
for reference.

If you want to inspect the internals for debugging reasons, you can enable debugging and use the two debug functions exposed by the library:

#define CIRCULAR_BUFFER_DEBUG
#include <CircularBuffer.h>
CircularBuffer<long, 6> buffer;

And then you can call buffer.debug(Serial) or buffer.debug(Serial, customFunction) to see the whole buffer contents with the head and tail positions.

@rlogiacco rlogiacco removed the waiting-feedback Waiting for feedback from the submitter label Dec 6, 2021
@pkrssy
Copy link
Author

pkrssy commented Dec 6, 2021

You're right but for a simple user like me it would be more comfortable to indicate the data is not available (return "nan") without the need to add additional conditions in a for cycle.

@rlogiacco
Copy link
Owner

rlogiacco commented Dec 7, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants