简体   繁体   English

获取有关使用未初始化 memory 的错误代码

[英]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.

相关问题 为什么我在以下代码段中收到UMR(未初始化的内存读取)错误 - Why I am getting UMR(unInitialized Memory Read)Error in following code snippet 为什么在尝试使 i = i*i 时出现错误“使用未初始化的 memory 'i'”和“使用未初始化的局部变量 'i'” - Why am I getting the error “using uninitialized memory 'i'” and “uninitialized local variable 'i' used” when trying to make i = i*i 为什么Visual C ++ 2010抱怨“使用未初始化的内存”? - Why is Visual C++ 2010 complaining about 'Using uninitialized memory'? VS2022代码分析错误C6001 Using uninitialized memory - VS2022 code analysis error C6001 Using uninitialized memory 在编译时获取未初始化的内存警告 - Getting uninitialized memory warning at compilation 对变量 ans 使用未初始化的 memory - Using uninitialized memory for variable ans 正在写入由分配器 UB 分配的未初始化的 memory 吗? 之后阅读它呢? - Is writing to uninitialized memory allocated by allocator UB? And what about reading it afterwards? C ++中未初始化的int错误代码 - uninitialized int error code in C++ 检查有关移动未初始化的变量并使用移动的变量 - checking about moving an uninitialized variable and using a moved variable Valgrind抱怨使用依赖于未初始化字节的条件跳转 - Valgrind complains about using conditional jumps that depend on uninitialized bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM