D
David Merposa
Hello,
We have some code that runs on Linux and Windows. The code uses a ring
buffer. To "increment" the index of the ring buffer, the following
construct is used:
index = ++index % N;
We recently had a review of this code, and my co-worker Tony and I
pointed out to the guy who wrote this code--Sporty--that it is undefined
behavior. The code currently "works" on both Linux and Windows, so he
told us that if it works, don't fix it.
While we agree with Sporty that it currently "works" on both Linux and
Windows, we're a bit concerned that it may not "work" in the future.
Sporty is like an old dog, and it is difficult if not impossible to
teach him "new tricks".
Here is an example program that demonstrates what I am talking about. It
outputs the same (currently "correct") results (3 and 0) on Linux and
Windows.
#include <stdio.h>
int main(void)
{
int x = 2;
x = ++x % 32;
printf("x = %d\n", x);
x = 31;
x = ++x % 32;
printf("x = %d\n", x);
return 0;
}
My question is: What to do about this? On the one hand, it's currently
not a problem. But on the other hand, it seems like a time bomb waiting
to go off.
One thing to keep in mind is that our management recently told us to
focus on the high priority tasks. Sporty says this is not a high
priority task, and that we're wasting time whenever we bring it up. I
fear that if we keep bringing this up, our jobs might be at risk. In
this day and age, jobs are hard to come by, so having a job is a pretty
fortunate thing. Still, it bugs me.
Thanks
We have some code that runs on Linux and Windows. The code uses a ring
buffer. To "increment" the index of the ring buffer, the following
construct is used:
index = ++index % N;
We recently had a review of this code, and my co-worker Tony and I
pointed out to the guy who wrote this code--Sporty--that it is undefined
behavior. The code currently "works" on both Linux and Windows, so he
told us that if it works, don't fix it.
While we agree with Sporty that it currently "works" on both Linux and
Windows, we're a bit concerned that it may not "work" in the future.
Sporty is like an old dog, and it is difficult if not impossible to
teach him "new tricks".
Here is an example program that demonstrates what I am talking about. It
outputs the same (currently "correct") results (3 and 0) on Linux and
Windows.
#include <stdio.h>
int main(void)
{
int x = 2;
x = ++x % 32;
printf("x = %d\n", x);
x = 31;
x = ++x % 32;
printf("x = %d\n", x);
return 0;
}
My question is: What to do about this? On the one hand, it's currently
not a problem. But on the other hand, it seems like a time bomb waiting
to go off.
One thing to keep in mind is that our management recently told us to
focus on the high priority tasks. Sporty says this is not a high
priority task, and that we're wasting time whenever we bring it up. I
fear that if we keep bringing this up, our jobs might be at risk. In
this day and age, jobs are hard to come by, so having a job is a pretty
fortunate thing. Still, it bugs me.
Thanks