Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:36:44+00:00 2026-06-14T05:36:44+00:00

I’m working on an simple vector program as an assignment, but I cant figure

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:36:46+00:00Added an answer on June 14, 2026 at 5:36 am
    assert(testVec=NULL);
    

    will set testVec to NULL and 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 == testVec rather than testVec == 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_vec nulls out its local copy of the Vector*, 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 a Vector*& (a reference to a pointer). Before you do that, though, you have another assignment-instead-of-comparison to fix: if (dealloc=NULL) should be if (dealloc == NULL).

    What’s more, the delete[] tempvec; in extend_vec frees memory you’re still using. You’re begging for segfaults and heap corruption if you keep it. So delete it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.