c++ - 我怎样才能安全地将 float *x 变成 const float *const *y?

标签 c++ pointers constants

我有一个函数 audioReceived (float * input, int bufferSize, int nChannels) 在其中我想从需要 const float *const *inputBuffers< 的库中调用一个函数.

显然转换 const float *const *inputBuffers = (const float* const*)input; 编译但是是一个糟糕的想法,程序崩溃,杀死小猫等。没有人需要修改原始 float* input,它是正在处理的传入音频数据。

如何以正确的方式做到这一点?

编辑:这里还有一些代码。 audioReceived 是:

void testApp::audioReceived (float * input, int bufferSize, int nChannels){ 

     Vamp::RealTime rt = Vamp::RealTime::fromMilliseconds(ofGetSystemTime());
     float const *const tmp[] = { input, 0 };    
     Vamp::Plugin::FeatureSet fs = myPlugin->process(tmp, rt);
 }

库函数process实际上定义在基类中:

 /**
 * Process a single block of input data.
 * 
 * If the plugin's inputDomain is TimeDomain, inputBuffers will
 * point to one array of floats per input channel, and each of
 * these arrays will contain blockSize consecutive audio samples
 * (the host will zero-pad as necessary).  The timestamp in this
 * case will be the real time in seconds of the start of the
 * supplied block of samples.
 *
 * If the plugin's inputDomain is FrequencyDomain, inputBuffers
 * will point to one array of floats per input channel, and each
 * of these arrays will contain blockSize/2+1 consecutive pairs of
 * real and imaginary component floats corresponding to bins
 * 0..(blockSize/2) of the FFT output.  That is, bin 0 (the first
 * pair of floats) contains the DC output, up to bin blockSize/2
 * which contains the Nyquist-frequency output.  There will
 * therefore be blockSize+2 floats per channel in total.  The
 * timestamp will be the real time in seconds of the centre of the
 * FFT input window (i.e. the very first block passed to process
 * might contain the FFT of half a block of zero samples and the
 * first half-block of the actual data, with a timestamp of zero).
 *
 * Return any features that have become available after this
 * process call.  (These do not necessarily have to fall within
 * the process block, except for OneSamplePerStep outputs.)
 */
virtual FeatureSet process(const float *const *inputBuffers,
               RealTime timestamp) = 0;

在实际的标题中:

FeatureSet process(const float *const *inputBuffers, Vamp::RealTime timestamp);

我认为 EXC_BAD_ACCESS 可能源于库函数需要一个零填充数组,而我没有给它一个。 (a) 这听起来合理吗,以及 (b) 如果是这样,是时候提出不同的 SO 问题了吗?

感谢大家到目前为止的帮助,非常有启发性/澄清性/教育性/有趣。

最佳答案

语义在这里很重要。从参数名称,我可以猜到你要调用的函数接受多个缓冲区,所以它需要一个 float 指针数组(即数组数组)。由于您只有一个数组,因此您需要创建一个包含原始指针的数组,并将其传递给函数。

如果函数有一个单独的参数来表示要传递的数组的长度(即缓冲区的数量),则使用一元 & 运算符获取地址并传递长度为 1应该足够了,否则你需要创建一个临时的空终止数组:

float const *const tmp[] = { input, 0 };

并将其传递给函数。

关于c++ - 我怎样才能安全地将 float *x 变成 const float *const *y?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6028473/

相关文章:

c++ - 测试一个类是否是多态的

c++ - 链接器错误(lnk2005、lnk1169)

c - feof(FILE *) 内置函数中的段错误

c++ - 在 BOOST_CHECK_EQUAL 期间隐藏 cerr 输出

c++ - 在 .EXE 路径中找不到 DLL

c++ - 指向整数数组的指针 C++

c++ - 将内存池中的malloc和free替换为new和delete

c++ - 在引用定义中使用 constexpr 和 const

elixir - 在 Elixir 中,有没有办法使 _real_ 常量?

c - 为什么可以使用 scanf 更改 const int?