【发布时间】:2018-09-24 13:31:11
【问题描述】:
这是正确的吗?
stereoBuffer 将是一个指针浮点类型的数组,大小为 mixQty(即 2);
// C++ in Header File
float **stereoBuffer;
// in cpp file inside a function of init
mixQty = 2; // this will be passed in function
stereoBuffer = new float*[mixQty];
for (int i = 0; i < mixQty; ++i) {
stereoBuffer[i] = (float *)malloc(samplerate * 2 * sizeof(float) + 32768);
}
帮助和详细回答将获得奖励。
【问题讨论】:
-
是 C 还是 C++?在 C++ 中,您应该避免使用
malloc(以及现代 C++ 中的new)。 -
为什么要混合使用
new(C++) 和malloc(C) ? -
在 C++ 中更喜欢
std::vector<std::vector<float>>而不是float**。 -
@KathanShah
malloc在 C 中使用,并且在 C++ 中支持主要是为了兼容性。在引入智能指针之前,new在 C++ 中用作替代方案。现在,std::make_unique和std::make_shared是动态分配的首选。虽然在这种情况下,只需使用std::vector就可以了,没有理由手动分配内存。 -
@KathanShah 可以,您只需将指向所有子向量的指针存储在
vector<float*>中,然后将该向量的data传递给函数。