[英]getting an error code about using uninitialized memory
I am using visual studio to code with, and I do not understand why i am getting the error "Error C6001 - Using uninitialized memory '*tempPtr' " at only the line right below the else if condition statement.我正在使用 Visual Studio 进行编码,但我不明白为什么我在 else if 条件语句正下方的行中收到错误“错误 C6001 - 使用未初始化的 memory '*tempPtr'”。
void removeNumber(double*& arrayPtr, double number, int& size) {
bool found = false;
double* tempPtr = new double[size-1];
for (int i = 0; i < size; i++) {
if (arrayPtr[i] == number) {
found = true;
}
else if (found == false && arrayPtr[i] != number) {
arrayPtr[i] = tempPtr[i];
}
else {
arrayPtr[i] = tempPtr[i - 1];
}
} delete[] arrayPtr;
arrayPtr = tempPtr;
--size;
}
arrayPtr[i] = tempPtr[i];
In this line, you try to assign an uninitialized tempPtr[i] to an initialized arrayPtr[i].在这一行中,您尝试将未初始化的 tempPtr[i] 分配给已初始化的 arrayPtr[i]。 It should be the other way around.应该反过来。 However, if what you want to do is to assign a null pointer to arrayPtr[i], you can initialize tempPtr with null pointers.但是,如果您想要将 null 指针分配给 arrayPtr[i],则可以使用 null 指针初始化 tempPtr。
This is not related to the question but I notice at the end you de-allocate the memory for arrayPtr and then use arrayPtr.这与问题无关,但我注意到最后您为 arrayPtr 取消分配 memory,然后使用 arrayPtr。 This will crash your program at run-time.这将使您的程序在运行时崩溃。 You only want to de-allocate when you have no use for it anymore.您只想在不再使用它时取消分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.