I’m working on an simple vector program as an assignment, but I cant figure out my why program asserts. My program compiles successfully, but fails at runtime. I think im at the reach of expertise on this one.
#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;
}
**EDIT
Some people asked me which assertion error I got:
Debug Assertion Failed!
Program:
…12\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe
File:f:\dd\vctools\crt_bld\self_x86\crt\src\dggdel.cpp
Line:52
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
I’ve also made the following changes:
assert(testVec=NULL)
(deAlloc==NULL)
to
assert(testVec==NULL)
(deAlloc==NULL)
this function from:
void dealloc_vec(Vector *deAlloc)
to:
void dealloc_vec(Vector *&deAlloc)
Assertion error fixed, however doesnt produce output. Still working on the debugging.
Also, quite possible this is more C than C++. My prof states in the assignment spec that this is C++, but he switches between the two lots in our class.
will set
testVectoNULLand evaluate as equal to that null pointer, which is false when treated as a boolean.That should almost certainly be
assert(testVec == NULL);instead.For future reference, this is why so-called “Yoda conditions” (
NULL == testVecrather thantestVec == NULL) are sometimes preferred in C and C++ when comparing to a constant. If you accidentally use=rather than==, Yoda conditions fail to compile, making the problem more obvious.With that fixed, you have another problem:
dealloc_vecnulls out its local copy of theVector*, but that’s just a copy of the real pointer; the change doesn’t make it back to the caller. You might want to declare the function to take aVector*&(a reference to a pointer). Before you do that, though, you have another assignment-instead-of-comparison to fix:if (dealloc=NULL)should beif (dealloc == NULL).What’s more, the
delete[] tempvec;inextend_vecfrees memory you’re still using. You’re begging for segfaults and heap corruption if you keep it. So delete it.