【发布时间】:2012-01-19 17:31:59
【问题描述】:
我非常熟悉 Java,这是允许的。但是它看起来不像C++。我在尝试分配 valuesToGrab = updatesValues; 时收到“无效的数组分配”。
//these are class attributes
int updatingValues[361] = {0};
int valuesToGrab[361] = {0};
//this is part of a function that is causing an error.
for (unsigned int i=0; i < 10; i++) {
//this fills values with 361 ints, and num_values gets set to 361.
sick_lms.GetSickScan(values,num_values);
//values has 361 ints, but a size of 2882, so I copy all the ints to an array
//of size 361 to "trim" the array.
for(int z = 0; z < num_values; z++){
updatingValues[z] = values[z];
}
//now I want to assign it to valuesToGrab (another program will be
//constantly grabbing this array, and it can't grab it while it's being
//populated above or there will be issues
valuesToGrab = updatingValues; // THROWING ERROR
}
我不想遍历updateValues并将其一一添加到valuesToGrab,但如果我必须这样做的话。有没有办法可以用 C++ 在一个函数中分配它?
谢谢,
【问题讨论】:
-
“修剪”数组似乎完全没有必要。修剪然后复制,双重如此。
-
你真的,真的应该学习 C & C++ 中的内存管理。
-
另外,你的评论“//现在我想将它分配给 valuesToGrab(另一个程序将 //不断地抓取这个数组,并且它在上面填充时无法抓取它或会有问题”强烈暗示需要通过互斥锁或关键部分进行进程同步。
-
谢谢你们。我同意修剪是不必要的,我知道有时我必须研究使用 C++ 进行线程化。我还听说内存管理在 C++ 中非常重要,所以我也会研究一下。
标签: c++ arrays variable-assignment