Thread: array index resetting itself

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    486

    array index resetting itself

    I have the following loop in part of my code, which is supposed to traverse an array backwards. When the index reaches 0, it resets itself and the loop starts over from the top. I've been staring at it so long I am probably just missing the obvious, so I'm hoping someone with fresh eyes can spot why this is happening.

    The offending loop is in bold, I include the rest for the sake of completeness but the first loop appears to be doing its job (or at least, it's not obviously not doing it's job, I'll know for sure when I can test the whole thing). If anyone is interested, this is the main part of a recursive digital low-pass filter.

    Code:
    void filter_signal(double *signal, double *tempfiltered, double *filtered, double *dcof, int *ccof, double scale, uint64_t order, uint64_t length)
    {
        uint64_t i,p,index;
        i = 0;
        p = 0;
        for (i=0; i<order; i++) //pad the output signal
        {
            tempfiltered[i] = signal[0];
        }
        for (i=0; i<length; i++)
        {
            tempfiltered[i] = ccof[0]*scale*signal[i];
            for (p=1; p<=order; p++)
            {
                if (i-p < 0)
                {
                    index = 0;
                }
                else
                {
                    index = i-p;
                }
                tempfiltered[i] += ccof[p]*scale*signal[index] - dcof[p]*tempfiltered[index];
            }
        }
        for (i=length-1; i>length-order-1; i--)
        {
            filtered[i] = tempfiltered[length-1];
        }
        for (i=0; i<length; i++)
        {
            printf("length is %"PRIu64" and i is %"PRIu64" and we are indexing position %"PRIu64"\n",length, i, length - i);
            fflush(stdout);
            filtered[length-i-1] = ccof[0]*scale*tempfiltered[length-i-1];
            for (p=1; p<=order; p++)
            {
                if (length-1-i+p > length-1)
                {
                    index = length-1;
                }
                else
                {
                    index = i+p;
                }
                filtered[length-i-1] += ccof[p]*scale*tempfiltered[index] - dcof[p]*filtered[index];
            }
        }
    }
    Last edited by KBriggs; 12-12-2014 at 09:30 AM.
    C is fun

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    EDIT: ...aaaand my code magically fixed itself. Undefined behavior, anyone?
    C is fun

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    What are the dimensions of the arrays being provided by the caller?

    The statement "filtered[length-i-1] += ccof[p]*scale*tempfiltered[index] - dcof[p]*filtered[index]" relies on

    1) "length-i + 1" being a valid index into array "filtered" (i.e. between 0 and the <length of filtered> - 1)
    2) "p" being a valid index of "ccof" (i.e. p between 0 and <length of ccof>-1).
    3) "p" being a valid index of "dcof"
    4) "index" being a valid index of "tempfiltered" and of "filtered"

    Given the way p is running from 1 to order, and the way index is being computed, I'm particularly suspicious of 2,3, and 4 (i.e. I'll bet your code has multiple problems with indices being out of array bounds). Check all of them.

    Print out all of those values (immediately before that statement) and compare them with the actual sizes of the arrays.

    I also suspect you have similar problems in the first loop of your function (the one not highlighted). You're just getting unlucky and symptoms are not immediately visible.




    Code with undefined behaviour does not fix itself (magically edit itself to eliminate undefined behaviour). The danger of undefined behaviour is that symptoms are often not consistent (e.g. can vary over time, depending on other problems in your code).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    filtered, tempfiltered, and signal all have length elements
    ccof and cdof bothhave order+1 elements

    so I think all those criteria are satisfied. Those were the first things I checked. I could see if for example the loop index was going under zero and resettings since it an in unsigned type or something, but then I would get a segfault. Instead, it starts at 0, goes to the end of the array, then immediately resets to 0 again. Somehow it is limiting itself to valid array indices and I don't understand why. Undefined behavior, sure, but for it to coincidentally reset itself once it hits precisely the length of the array every time seems a little too regular to be memory trashing or array out of bounds issues.
    C is fun

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    unsigned variables work with modulo arithmetic.

    Subtract 1 from 0U, and the result is the maximum value the variable can hold.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I know. I'm fairly certain that's not happening here, based on my previous post and printing the values during execution.

    EDIT: I think maybe the problem is with the calculation of index. It doesn't explain why the loop index resets itself, but maybe that's just the undefined part of it.

    Code:
    if (length-1-i+p > length-1)
    {
            index = length-1;
    }
    else
    {
            index = i+p;
    }
    should be
    Code:
    if (i+p > length-1)
    {
            index = length-1;
    }
    else
    {
            index = i+p;
    }
    Last edited by KBriggs; 12-12-2014 at 09:52 PM.
    C is fun

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by KBriggs View Post
    EDIT: I think maybe the problem is with the calculation of index.
    Duh!!! That is what I was hinting in my previous post.

    Quote Originally Posted by KBriggs View Post
    It doesn't explain why the loop index resets itself, but maybe that's just the undefined part of it.
    Your description here is wildly unspecific, but you have shown no inclination to be more specific.

    The only way your code, as it stands, would exhibit undefined behaviour is if it was accessing elements out of bounds. And you claim that is not the case.

    The fact that unsigned types work by modulo arithmetic is well defined by the standard. That doesn't mean the values you are using to index arrays will be valid.

    If you are getting a value you don't expect for an index, it means you don't properly understand arithmetic operations. Or your claim of accessing valid array indices is incorrect.

    The fact you're now changing your calculation of index is just a hint you're guessing. Have fun. I'm bowing out of this thread.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I'm not being more specific because I'm not at work so I don't have the code in front of me to test as I write this. So yes, I am guessing (thanks for focusing my guess) and I will actually be able to be more specific when I test the guess...
    C is fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calloc for an array of array with negative index in C
    By george_engineer in forum C Programming
    Replies: 6
    Last Post: 10-15-2014, 03:26 AM
  2. Replies: 1
    Last Post: 10-13-2013, 12:01 AM
  3. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  4. Resetting an array
    By Canadian0469 in forum C Programming
    Replies: 5
    Last Post: 11-12-2007, 11:36 AM
  5. Resetting a character Array
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-17-2002, 01:04 PM