Skip to content

Commit

Permalink
fixed undefined behaviour (changing twice between sequence points
Browse files Browse the repository at this point in the history
  • Loading branch information
robosam2003 committed Aug 19, 2022
1 parent ab84022 commit d58dada
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ For this reason, a check before popping is vital.
(exampleFifo.fifo_status() == Fifo_STATUS::Fifo_EMPTY) ? /* Handle the fact that it's empty. */ : exampleFifo.pop();
```

See `example.cpp` for a more examples.
See `example.cpp` for more examples.


### Usage with Vector
Expand Down
8 changes: 5 additions & 3 deletions dynamicFifo.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ Fifo_STATUS Fifo<T>::push(const T& item) {
}
// otherwise:
elem[nextFree] = item;
if (((nextFree+1) % sz) == endPointer) {
if (((nextFree+1) % sz) == static_cast<unsigned int>(endPointer)) {
nextFree = -1;
}
else{
nextFree = (++nextFree) % sz; // wrap around /:)
nextFree++;
nextFree = (nextFree) % sz; // wrap around /:)
}
return Fifo_STATUS::Fifo_GOOD;
}
Expand All @@ -75,7 +76,8 @@ T Fifo<T>::pop() { /// Note: You should check the status of the fifo before call
if (fifo_status()==Fifo_STATUS::Fifo_FULL) {
nextFree=endPointer;
}
endPointer = (++endPointer) % sz; // wrap around /:)
endPointer++;
endPointer = (endPointer) % sz; // wrap around /:)
return r;
}

Expand Down
6 changes: 5 additions & 1 deletion example.cpp → examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ int main() {
// C-style casts. - does not resize the fifo.
Fifo<double> e2 = (Fifo<double>) e1;

(e2.fifo_status() == Fifo_STATUS::Fifo_EMPTY) ? /* Handle the fact that it's empty. */ : e2.pop();

if (e2.fifo_status() == Fifo_STATUS::Fifo_EMPTY)
std::cout<<"It's empty";
else
e2.pop();
std::cout << "e2 Free space: " << e2.free_space() << std::endl;

#endif // DYNAMIC_FIFO_H
Expand Down

0 comments on commit d58dada

Please sign in to comment.