问题
I am doing an Windows application using C++. I have the next dude.
I have this function where I receive a void * with data audio capture:
void Pre_proc_mono::PreProcess(void *data, int lenbytes) {
SLData_t *signal1;
SLData_t *signal2;
SLData_t *result;
signal1 = (SLData_t*)data;
signal2 = new SLData_t[lenbytes];
result = new SLData_t[2 * lenbytes - 1];
for (int i = 0; i < lenbytes; i++){
signal2[i] = signal1[i];
}
}
The loop always fail around 11000, why? What am I doing wrong ? The idea is then do a correlation cross with SigLib library (DSP library). So I need have arrays with limit, not pointers so I am doing this instead. Help?
EDIT: First, when I said fails I want to say that when loop reaches to around 11000 appears and runtime error. Second SLData_t is a kind of date of the SigLib library, and the function to doing correlation cross need this variable as input.
回答1:
Following may help:
void Pre_proc_mono::PreProcess(void *data, int lenbytes) {
SLData_t* signal1 = reinterpret_cast<SLData_t*>(data);
const std::size_t signalCount = lenbytes / sizeof (SLData_t);
// then if you want to copy inside a std::vector
std::vector<SLData_t> signal2(signal1, signal1 + signalCount);
}
回答2:
This could be implemented far better, safe, and simple using std::vector instead of manual-memory-management and std::copy():
void Pre_proc_mono::PreProcess(void *data, std::size_t lenbytes){
SLData_t *signal1 = (SLData_t*)data;
std::size_t data_length = lenbytes / sizeof( SLData_t );
std::vector<SLData_t> signal2;
std::vector<SLData_t> result;
std::copy( std::begin( signal1 ) ,
std::end( signal1 ) + data_length ,
std::back_inserter( signal2 )
);
}
来源:https://stackoverflow.com/questions/20838389/copy-void-content-into-siglib-struct-sldata-t