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
