Can';我不明白为什么我的程序会断言

Can';我不明白为什么我的程序会断言,c,memory,assertion,C,Memory,Assertion,我正在做一个简单的向量程序作为作业,但我不明白为什么我的程序会断言。我的程序编译成功,但在运行时失败。我想我已经具备了这方面的专业知识 #include <iostream> #include <cstring> #include <assert.h> #include <stdio.h> #include <iomanip> #define TESTING using namespace std; typedef float Elem

我正在做一个简单的向量程序作为作业,但我不明白为什么我的程序会断言。我的程序编译成功,但在运行时失败。我想我已经具备了这方面的专业知识

#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;
}
此功能来自: void dealoc_vec(向量*dealoc)

致:

void dealoc_vec(Vector*&dealoc)

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

也有可能,这比C++更为C。我的教授在作业说明书中说这是C++,但他在我们班的两个批次之间切换。

assert(testVec=NULL);
testVec
设置为
NULL
,并计算为等于该NULL指针,该指针被视为布尔值时为false

几乎可以肯定的是
assert(testVec==NULL)取而代之

<> P>为将来的引用,这就是为什么在比较常数时,有时在C和C++中优先考虑所谓的“尤达条件”(<代码> null = TestVECEC < /COD>而不是<代码> TESTVEC= = null < /COD> >)。如果您不小心使用了
=
而不是
=
,那么Yoda条件将无法编译,从而使问题更加明显

解决了这个问题后,您会遇到另一个问题:
dealloc\u vec
将其
Vector*
的本地副本置零,但这只是实际指针的副本;更改不会返回给来电者。您可能希望声明函数采用
向量*&
(对指针的引用)。但是,在执行此操作之前,您需要修复另一个赋值而不是比较:
if(dealoc=NULL)
应该是
if(dealoc==NULL)

此外,
delete[]tempvecextend\u vec
中的code>释放您仍在使用的内存。如果你保留它,你是在乞求分段错误和堆损坏。所以删除它

testVec
设置为
NULL
,并计算为等于该NULL指针,该指针被视为布尔值时为false

几乎可以肯定的是
assert(testVec==NULL)取而代之

<> P>为将来的引用,这就是为什么在比较常数时,有时在C和C++中优先考虑所谓的“尤达条件”(<代码> null = TestVECEC < /COD>而不是<代码> TESTVEC= = null < /COD> >)。如果您不小心使用了
=
而不是
=
,那么Yoda条件将无法编译,从而使问题更加明显

解决了这个问题后,您会遇到另一个问题:
dealloc\u vec
将其
Vector*
的本地副本置零,但这只是实际指针的副本;更改不会返回给来电者。您可能希望声明函数采用
向量*&
(对指针的引用)。但是,在执行此操作之前,您需要修复另一个赋值而不是比较:
if(dealoc=NULL)
应该是
if(dealoc==NULL)


此外,
delete[]tempvecextend\u vec
中的code>释放您仍在使用的内存。如果你保留它,你是在乞求分段错误和堆损坏。所以请删除它。

请发布错误消息。可能有人可以帮助你没有时间来复制/粘贴/编译/运行你的代码,并看到他们自己的错误信息,所以说,这不是C++。这是C++编译的C语言。您在
main
中声明的那些函数?这些应该是
Vector
的成员函数:我们的整个方法都是错误的。在每个函数中不断创建新的向量,并在testVec的内容上复制它们的内容。您正在查找各种内存,因为您从未删除从
alloc\u vec
extend\u vec
返回的向量。你应该编写函数或成员函数来修改向量,而不是返回需要复制的新向量。因为你确切地知道哪一行是断言的,如果你能在你的问题中包含这一点,那将非常有帮助。它极大地简化了事情,并将有助于避免我们费力地阅读几十行代码(并将更快地得到答案)。将来发帖时,请记住,如果你有信息让你的问题更清楚,你应该把它包括在内。“你的整个方法都被误导了。在每个函数中不断创建新的向量,并在testVec的内容上复制它们的内容。你在寻找各种各样的内存,因为你从不删除从alloc_vec和extend_vec返回的向量。您应该编写修改向量的函数或成员函数,而不是返回需要复制的新向量巴尔马“我正在按照作业规范工作。这些是我的教授要求的函数,这是他要求测试的类型。请发布错误消息。可能有人可以帮助你没有时间来复制/粘贴/编译/运行你的代码,并看到他们自己的错误信息,所以说,这不是C++。这是C++编译的C语言。您在
main
中声明的那些函数?这些应该是
Vector
的成员函数:我们的整个方法都是错误的。在每个函数中不断创建新的向量,并在testVec的内容上复制它们的内容。您正在查找各种内存,因为您从未删除从
alloc\u vec
extend\u vec
返回的向量。你应该编写函数或成员函数来修改向量,而不是返回需要复制的新向量。因为你确切地知道哪一行是断言的,如果你能在你的问题中包含这一点,那将非常有帮助。它极大地简化了事情,并将有助于避免我们费力地阅读几十行代码(并将更快地得到答案)。将来发帖时,请记住,如果你有信息让你的问题更清楚,你应该把它包括在内
assert(testVec=NULL);