0

I was hoping the return values in c++11 are already moved or optimized out as explained in here: c++11 Return value optimization or move?
However I am having this exception below.
"inference(4160,0x7fffb832e380) malloc: error for object 0x7fc415f00a10: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug".
The code:

vector<double> sp_peaks = dsp.diff(zero_sp_cropped);
sp_peaks = dsp.sign(sp_peaks);
sp_peaks = dsp.diff(sp_peaks);

The third line is giving the error.
The sign and diff methods are below.

vector<double> SignalProcessing::diff(vector<double> &signal) {
    /*
     * Performs the diff filter on the input signal and returns the value.
     * The returned signal has length 1 less than the input signal.
     * */

    vector<double> res(signal.size()-1); // -1 because the first one cannot have a diff

    for (int i = 1; i < signal.size(); ++i) {
        res[i] = signal[i]-signal[i-1];
    }
    return res;
}

vector<double> SignalProcessing::sign(vector<double> &signal) {
    /*
     * Maps the input signal into a signal of {-1.0,1.0} values by their sign values.
     * Positive values will be mapped to 1.0 while negative will be mapped to -1.0
     * */
    vector<double> res(signal.size());

    for (int i = 0; i < signal.size(); ++i) {
        if (signal[i] > 0)
            res[i] = 1.0;
        else
            res[i] = -1.0;
    }
    return res;
}

The error disappears if I place a breakpoint and debug. Could you explain why this is happening and what is a good practice of returning an std container?
Thank you in advance!
Anil

anilbey
  • 1,817
  • 4
  • 22
  • 38
  • 6
    Off by one error. The size of the vector is `res(signal.size()-1)` but you loop till `i < signal.size()`, not `i < signal.size() - 1` – NathanOliver Oct 24 '18 at 20:20
  • No, that part is correct. I start i from one. That was intentional. – anilbey Oct 24 '18 at 20:21
  • 3
    then you need `res[i-1] = signal[i]-signal[i-1];` because say signal is size=2 then res has 1 element -- BUT it's at index 0, but you try to put it into index 1 – xaxxon Oct 24 '18 at 20:21
  • 1
    But you do `res[i]` in the loop and `res[i]` does not exist when `i == signal.size() - 1` – NathanOliver Oct 24 '18 at 20:22
  • 2
    One of the first things you can do when trying to debug code like this is add `assert`s before every array access to catch any attempt at an out of bounds access. That would have found this problem easily. Or you could even have just used [at](https://en.cppreference.com/w/cpp/container/array/at) which exists for this purpose. – David Schwartz Oct 24 '18 at 20:23
  • 3
    @DavidSchwartz or turn on your STL's debug macros.. or just use `.at` instead of `[]` – xaxxon Oct 24 '18 at 20:23
  • You are right, that is indeed an error! – anilbey Oct 24 '18 at 20:24
  • Alright, the exception is gone. It was due to what @NathanOliver pointed out. Thanks Nathan! – anilbey Oct 24 '18 at 20:42
  • I believe this is the 3rd off by one array access bug today. Should these be answered or is there a good duplicate or should they be closed as a typo? – drescherjm Oct 24 '18 at 20:57
  • 1
    Off by one exists in murk-space. Be hard to make a generic answer to use as a landing pad without it being overly broad (and in my opinion useless) like the generic Unresolved External Q&A. The big take-away is every time you THINK you are correctly using one-based indexing, you probably aren't. – user4581301 Oct 24 '18 at 21:15

0 Answers0