首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何安全地将一个浮点数*x变成一个const浮点数?

我如何安全地将一个浮点数*x变成一个const浮点数?
EN

Stack Overflow用户
提问于 2011-05-17 08:55:58
回答 4查看 4K关注 0票数 4

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

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

我该怎么做才是对的?

编辑:下面是一些更多的代码。 audioReceived是:

代码语言:javascript
复制
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实际上是在基类中定义的:

代码语言:javascript
复制
 /**
 * 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;

在实际标题中:

代码语言:javascript
复制
FeatureSet process(const float *const *inputBuffers, Vamp::RealTime timestamp);

我认为EXC_BAD_ACCESS可能源于库函数需要一个零填充数组,而我没有给它一个。(a)这听起来是否合理;及(b)若然,是否应该提出不同的问题?

到目前为止,谢谢大家的帮助,这是非常enlightening/clarifying/educational/interesting.

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-05-17 09:05:02

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

如果函数对它要传递的数组的长度有一个单独的参数(即缓冲区的数目),那么使用一元&运算符并传递1的地址就足够了,否则您需要创建一个临时的以空结尾的数组:

代码语言:javascript
复制
float const *const tmp[] = { input, 0 };

然后把它传递给函数。

票数 3
EN

Stack Overflow用户

发布于 2011-05-17 09:02:11

对演员本身来说,做&input应该足够了。注意,内部函数的参数是指向指针的指针。

编辑:

若要获得对原始问题的注释中要求的以空结尾的输入缓冲区列表,您可以使用:

代码语言:javascript
复制
float const * const buffers[] = {input, 0};
票数 2
EN

Stack Overflow用户

发布于 2011-05-17 09:02:31

代码语言:javascript
复制
float *

代码语言:javascript
复制
float **

因此,将input转换为inputBuffers是行不通的。

通常,从非const到const的转换是隐式的,您不需要做任何特殊的事情。你不能从康斯特到非康斯特那么容易。这是合乎逻辑的,当你考虑它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6028473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档