Gcc linux vs windows compiler


#1

I just found a problem (see dsp filters in General Juce Discussions).

 

I was using the Vinniefalco DSP Filters and devoped first on windows 32 bit.  They worked great.  Then I tried porting my work to KXStudio (Ubuntu linux 64 bit).  Everything worked but the filters were very ratty/noise/distorted.  It took me quite some time to figure would what was wrong.   I found this with a oscilloscope and a marker inserted by my code to see buffer bounderies in the output wave form. I finally traced it to this line of code:

 while (--numSamples >= 0)

     *dest++ = state.process(*dest, *this);

 

The problem here is that the value passed to the process function is post increment of the dest.  By looking at it it would not appear that it should work that way but it does with 64bit gcc.

The code would skip the first sample in the buffer, and process 1 passed the end.  The resulting buffer endsup shifted 1 sample earlier

I changed the code to this:

while (--numSamples >= 0)

{

      *dest = state.process(*dest, *this);

      dest++;

}

This solved the problem

In searching the net I found that the behaviour of this is 'undefined' and is compiler dependant.  So I suggest you don't code like this or your code may not be portable to all platforms.  I think the reason is the increment is on the left side of the operator and the variable is consumed on the right.  I asume some compilers evaluate the left side, push it to the stack to be consumed by the right while other compilers evaluate the right side first the evaluate the left side.  (not a compiler expert but this is how 'I think' it works).