[英]Populating argument of float array as float* in C++
I have this function like this: 我有这样的功能:
bool funcFloats(float* output) {
output = new float[100];
populateArray((float *) &(*firstArray)[0], output); //First array is an std::vector<uint8_t>
for(int i=0; i<100; i++) {
std::cout<<output[i]<<std::endl;
}
}
template <typename A, typename B> void populateArray(A *in, B *out)
{
using namespace std;
for (unsigned y = 0; y < 100; y++)
{
out[y] = in[y];
}
}
and I call it using this: 我用这个来称呼它:
float array[100];
obj.funcFloats(array);
for(int i=0; i<100; i++) {
std::cout<<array[i]<<std::endl;
}
and what's printed at the end of funcFloats is correct, but what's printed after the function is called is not. 并且在funcFloats末尾打印的内容是正确的,但是在调用该函数之后打印的内容却不正确。 Why is this? 为什么是这样?
This is because you assign output
inside the function, making the array passed into funcFloat
completely useless. 这是因为您在函数内部分配了output
,使得传递给funcFloat
的数组完全无用。 It remains uninitialized when funcFloat
exits, causing incorrect output. 当funcFloat
退出时,它保持未初始化状态,从而导致错误的输出。
Remove the assignment to output
from funcFloat
to fix this problem: 从funcFloat
删除output
的分配以解决此问题:
bool funcFloats(float* output) {
output = new float[100]; // <<== Remove this line
populateArray((float *) &(*firstArray)[0], output);
for(int i=0; i<100; i++) {
std::cout<<output[i]<<std::endl;
}
}
Although this would fix the immediate problem, the code would remain not C++-idiomatic. 尽管这可以解决当前的问题,但是代码仍然不是C ++惯用的。 You should stay away from "raw" C-style arrays whenever possible, using vectors instead. 您应尽可能避免使用“原始” C样式数组,而应使用向量。 This fixes multiple issues, such as the one you've seen, but also a possibility of array boundary mismatches (writing 1000 items into a 100-item array), possible memory leaks, and inability to grow an array dynamically. 这样可以解决多个问题,例如您所见过的一个问题,但也可能出现数组边界不匹配(将1000个项目写入100个项目的数组),可能的内存泄漏以及无法动态增长数组的问题。
bool funcFloats(float* output) { output = new float[100];
As already noted in another answer, you have a problem in the above output
assignment. 如另一个答案中所述,您在上述output
分配中存在问题。 You dynamically allocate a float
array with new[]
, you assign the pointer to the first array element to the output
variable, and doing so the original output
array passed as parameter is lost after the assignment. 您可以使用new[]
动态分配一个float
数组,然后将指向第一个数组元素的指针分配给output
变量,这样分配后丢失作为参数传递的原始 output
数组。
Note also that C++ doesn't have a garbage collector, so when you explicitly allocate memory with new[]
, you should have code that frees that memory with delete[]
. 还要注意,C ++没有垃圾收集器,因此,当您使用new[]
显式分配内存时,应该具有使用delete[]
释放该内存的代码。
A much better approach (that will make your code simpler), is to use std::vector<float>
instead of a raw C-style array. 更好的方法(这将使您的代码更简单)是使用std::vector<float>
而不是原始的C样式数组。 This standard vector container will automatically release its own memory (so you don't have to pay attention to code doing that explicitly). 这个标准的向量容器将自动释放其自己的内存(因此您不必注意显式执行此操作的代码)。 And, if you want to pass to your function an input/output array parameter, you can pass a (non-const) reference to std::vector
, eg: 而且,如果要将函数输入/输出数组参数传递给函数,则可以将(非const)引用传递给std::vector
,例如:
bool funcFloats(std::vector<float> & output) {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.