Skip to content

Commit d58dada

Browse files
committed
fixed undefined behaviour (changing twice between sequence points
1 parent ab84022 commit d58dada

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ For this reason, a check before popping is vital.
2929
(exampleFifo.fifo_status() == Fifo_STATUS::Fifo_EMPTY) ? /* Handle the fact that it's empty. */ : exampleFifo.pop();
3030
```
3131

32-
See `example.cpp` for a more examples.
32+
See `example.cpp` for more examples.
3333

3434

3535
### Usage with Vector

dynamicFifo.tpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ Fifo_STATUS Fifo<T>::push(const T& item) {
5656
}
5757
// otherwise:
5858
elem[nextFree] = item;
59-
if (((nextFree+1) % sz) == endPointer) {
59+
if (((nextFree+1) % sz) == static_cast<unsigned int>(endPointer)) {
6060
nextFree = -1;
6161
}
6262
else{
63-
nextFree = (++nextFree) % sz; // wrap around /:)
63+
nextFree++;
64+
nextFree = (nextFree) % sz; // wrap around /:)
6465
}
6566
return Fifo_STATUS::Fifo_GOOD;
6667
}
@@ -75,7 +76,8 @@ T Fifo<T>::pop() { /// Note: You should check the status of the fifo before call
7576
if (fifo_status()==Fifo_STATUS::Fifo_FULL) {
7677
nextFree=endPointer;
7778
}
78-
endPointer = (++endPointer) % sz; // wrap around /:)
79+
endPointer++;
80+
endPointer = (endPointer) % sz; // wrap around /:)
7981
return r;
8082
}
8183

example.cpp examples/example.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ int main() {
8787
// C-style casts. - does not resize the fifo.
8888
Fifo<double> e2 = (Fifo<double>) e1;
8989

90-
(e2.fifo_status() == Fifo_STATUS::Fifo_EMPTY) ? /* Handle the fact that it's empty. */ : e2.pop();
90+
91+
if (e2.fifo_status() == Fifo_STATUS::Fifo_EMPTY)
92+
std::cout<<"It's empty";
93+
else
94+
e2.pop();
9195
std::cout << "e2 Free space: " << e2.free_space() << std::endl;
9296

9397
#endif // DYNAMIC_FIFO_H

0 commit comments

Comments
 (0)