无法弄清楚为什么我的程序断言

Kei*_*h k 0 c memory assertion

我正在研究一个简单的向量程序作为一个赋值,但我无法弄清楚程序断言的原因.我的程序编译成功,但在运行时失败.我认为即时通讯在这方面的专业知识.

#include <iostream>
#include <cstring>
#include <assert.h>
#include <stdio.h>
#include <iomanip>
#define TESTING
using namespace std;
typedef float Elem;//floats for vector elements

struct Vector{//structure for the vector
    unsigned int size;
    Elem *svector;
};


int main(){

#ifdef TESTING
        //prototypes
    Vector *alloc_vec();
    bool print_vec(Vector *printVector);
    Vector *extend_vec(Vector *extend,Elem element);
    Vector *scalar_plus(Vector *vecToAdd, Elem addElement);
    void dealloc_vec(Vector *&deAlloc);

    //testing scaffolds
    Vector *testVec=new Vector;
    *testVec=*alloc_vec();
    assert(testVec->size==0);
    assert(testVec->svector==NULL);

    for(int i=0;i=10;i++){
        *testVec=*extend_vec(testVec,Elem(i));
    }

    assert(testVec->size!=0);
    assert(testVec->svector!=NULL);

    assert(print_vec(testVec));
    print_vec(testVec);

    *testVec=*scalar_plus(testVec,5);

    print_vec(testVec);

    dealloc_vec(testVec);

    assert(testVec==NULL);
#endif //testing

    return 0;
}

Vector *alloc_vec(){//constructor to allocate an empty (zero-length) vector
    Vector *newVector=new Vector; //initiatizes a new vector
        if (newVector==NULL){
        return NULL;
    }

    newVector->size=0;//sets length to 0 
    newVector->svector=NULL;//sets vector to null


    return newVector;
}

bool print_vec(Vector *printVector){

    if(printVector==NULL){//makes sure printVector exists to pass unit test 1
        return false;
    }

    for(unsigned int i=0; i<printVector->size;i++){
        cout<<printVector->svector[i]<<endl;
    }

    return true;

}

void dealloc_vec(Vector *deAlloc){
    if (deAlloc==NULL){//if the vector contains no memory, no need to deallocate, unit test#1
        return;}

    delete deAlloc;//clears the memory of the vector
    deAlloc=NULL;
    return;

}

Vector *extend_vec(Vector *extend,Elem element){
    if (extend==NULL){
        return NULL;}


    Elem *tempVec=new Elem[extend->size+1];//sets up a temp vector one size larger
    tempVec[extend->size]=element;

    memcpy(tempVec,extend->svector,(extend->size*sizeof(Elem)));//copies the memory from the original array to the rest of the temp array

        extend->size+=1;

    delete[] extend->svector;//clears the memory

    extend->svector=tempVec;//the original vector now becomes the extended vector

    delete[] tempVec;//clears the temporary memory

        return extend;
}

Vector *scalar_plus(Vector *vecToAdd, Elem addElement){
    if (vecToAdd==NULL){
        return NULL;}
    for(unsigned int i=0;i<vecToAdd->size;i++){//adds a scalar to each element 
        vecToAdd->svector[i]+=addElement;
        }

        return vecToAdd;
}
Run Code Online (Sandbox Code Playgroud)

**编辑有人问我得到了哪个断言错误:

调试断言失败!

程序:... 12\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe

文件:F:\ DD\vctools\crt_bld\self_x86\CRT\SRC\dggdel.cpp

行:52

表达:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

我还做了以下更改:assert(testVec = NULL)(deAlloc == NULL)

assert(testVec==NULL)
(deAlloc==NULL)
Run Code Online (Sandbox Code Playgroud)

此函数来自:void dealloc_vec(Vector*deAlloc)

至:

void dealloc_vec(Vector*&deAlloc)

断言错误已修复,但不会产生输出.仍在进行调试.

而且,很可能这比C++更多.我的教授在作业规范中指出这是C++,但他在我们班级的两个地段之间切换.

cHa*_*Hao 6

assert(testVec=NULL);
Run Code Online (Sandbox Code Playgroud)

将设置testVecNULL并且评估为等于该空指针,当被视为布尔值时,该指针为false.

这几乎肯定是assert(testVec == NULL);相反的.

为了将来参考,这就是为什么在与常数比较时,在C和C++中有时优选所谓的"Yoda条件"(NULL == testVec而不是testVec == NULL).如果你不小心使用=而不是==,Yoda条件无法编译,使问题更加明显.

修复后,你有另一个问题: dealloc_vec将其本地副本空出来Vector*,但这只是真实指针的副本; 更改不会使其返回给调用者.您可能希望声明该函数采用Vector*&(对指针的引用).但是,在你这样做之前,你有另一个任务 - 而不是比较修复: if (dealloc=NULL)应该是if (dealloc == NULL).

更重要的是delete[] tempvec;,extend_vec释放你仍在使用的记忆.如果你保留它,你就是在乞求段错误和堆腐败.所以删除它.