'Input C++ Vector into C function

I have a std::vector<float> containing sound data. Without copying its data, I'd like to use this vector as input to the sonicChangeFloatSpeed function of the Sonic library. This method expects a float* as first argument and mutates the input array. After completion, the pointer in first argument would point to the result data.

data gives me access to the internal array of a C++ vector and with assign, I can replace the vector's contents. Hence, I tried the following:

float* ptr = vec.data();
int num_samples = sonicChangeFloatSpeed(ptr, vec.size(), 1., 1.5, 1., 1., 0, 41000, 1);
vec.assign(ptr, ptr + num_samples);

But when I run this program, I get the error double free or corruption (!prev) with a SIGABRT at this location. What is the problem of this approach and how would this question be solved more appropriately?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source