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

#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;
}

**编辑
有人问我得到了哪个断言错误:
调试断言失败!
程序:
…12\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe
文件:f:\dd\vctools\crt_bld\self_x86\crt\src\dggdel.cpp
线路:52
表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
我还做了以下更改:
断言(testVec=NULL)
(deAlloc==NULL)

assert(testVec==NULL)
(deAlloc==NULL)

此函数来自:
void dealloc_vec(向量*dealloc)
致:
void dealloc_vec(向量*&dealloc)
断言错误已修复,但不产生输出。仍在调试中。
而且,很可能这是比C++更多的C。我的教授在作业说明中说这是C++,但是他在我们班的两个批次之间切换。

最佳答案

assert(testVec=NULL);

testVec设置为NULL并计算为等于该空指针,当作为布尔值处理时为false。
几乎可以肯定的是,这应该是assert(testVec == NULL);
为了将来参考,这就是为什么在比较常量时,C和C++有时更喜欢所谓的“尤达条件”。如果不小心使用了NULL == testVec而不是testVec == NULL,Yoda条件将无法编译,从而使问题更加明显。
解决了这个问题,您还有一个问题:=将其==的本地副本置空,但这只是真正指针的副本;更改不会使其返回到调用方。您可能希望声明该函数以获取dealloc_vec(对指针的引用)。不过,在你这样做之前,你还有另一个任务要解决,而不是比较:Vector*应该是Vector*&
而且,if (dealloc=NULL)中的if (dealloc == NULL)释放您仍在使用的内存。如果你留着它,你是在乞求分段错误和堆腐败。所以删除它。

关于c - 无法弄清楚为什么我的程序会断言,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13205126/

10-15 12:14