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 7997581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:59:49+00:00 2026-06-04T14:59:49+00:00

I am getting garbage in my user defined vector.Garbage comes after erase function this

  • 0

I am getting garbage in my user defined vector.Garbage comes after erase function this is the code

    #if 1   // Make it 1 to enable this code

#include <iostream>
using namespace std;

class Vector{
protected:
    int l;
    int* v;
public:
    Vector():l(0),v(0){
        cout << "\nBase class: Vector: Default constructor" << endl;
    }
    Vector( int len ):l(len),v(new int[l]) {
        cout << "Vector: Overloaded constructor" << endl;
    }
    void set( int i, int val)
    {
        if( l )
            v[i] = val;
        else{
            cout << "Error: zero size vector\n exiting..." <<endl;
            exit(5);
        }
    }
    int get( int i)
    {
        if( l )
            return v[i];
        else{
            cout << "Error: zero size vector\n exiting..." <<endl;
            exit(5);
        }       
    }   
    ~Vector(){
        cout << "Base dextructor" <<endl;
        delete [] v;
    }

};

class Vector1:public Vector{
private:

public:
    Vector1():Vector(){
        cout << "Derived class: Vector1:: Default constructor" << endl;
    }
    //my functions
    int size()
    {
    return l;
    }
    int front()
    {
    return Vector::get(0);
    }
    int end()
    {
    return Vector::get(l-1);
    }
    void swap(int a,int b)
    {
    int temp=v[a];
    v[a]=v[b];
    v[b]=temp;
    }
    void find(int val)
    {
        bool flag=false;
       for(int i=0;i<l;i++)
       {
         if(v[i]==val)
         {
         flag=true;
         }
       }
       if(flag==true)
           cout<<"\nValue ="<<val<<" =Exists in Vector";
       else
           cout<<"\nValue ="<<val<<" =doesnot Exists in Vector";
    }
    void erase(int val)
    {

        int *temp=new int[l-1];
        int k=0;
        for(int i=0;i<l;i++)
        {
        if(v[i]!=val)
        {
        temp[i]=v[i];
        k++;
        }
        }
        delete []v;
        l=l-1;
        v=new int[l];
        for(int i=0;i<l;i++)
        {
        v[i]=temp[i];
        }



    }
    int resize( int len ) 
    {
        if( l )
            delete [] v;
        l = len;
        v = new int[l];
        return (v!=0);
    }
    void set( int i, int val)
    {
        if( i>= 0 && i<l )
            Vector::set( i, val );
        else{
            cout << "Error: index out of range\n exiting..." <<endl;
            exit(3);
        }
    }
    int get( int i)
    {
        if( i>= 0 && i<l )
            return Vector::get(i);
        else{
            cout << "Error: index out of range\n exiting..." <<endl;
            exit(3);
        }
    }   
    int & operator [](int i)
    {
        return v[i];
    }

    Vector1( int len ):Vector(len)
    {

    }

};

int main()
{
    Vector vec; 

    Vector1 v1;
    v1.resize( 4 );
    v1.set( 2, 4);
    v1.set( 1, 5);
    v1.set( 0, 6);

    int x = v1[2];

    v1[3] = 77;
    cout<<"\nSize of vector is=\n"<<v1.size();
    //v1.set( 5, 4);        // erroneous value
    cout<<"\nFront of vector is=\n"<<v1.front();
    cout<<"\nEnd of vector is=\n"<<v1.end();
   //do swap between values
    cout<<"\n";
    v1.swap(1,3);
    //now values are
    cout<<"v1[1] is equals to= "<<v1[1];
    cout<<"v1[3] is equals to= "<<v1[3];
    v1.find(5);
    v1.find(100);
    cout<<"\nNow v1[0] is equals to= \n"<<v1[0];
    v1.erase(6);
    cout<<"\nNow v1[0] is equals to= \n"<<v1[0]<<"  "<<v1[1];
    cout<<"\n";
}
#endif

Output is this
enter image description here

v1[0] should be be 5

  • 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-04T14:59:50+00:00Added an answer on June 4, 2026 at 2:59 pm

    In your erase function, you are not actually erasing the given number, you just copy every number in the vector that is not equal to the given number to a new array. E.g., if your internal array in the vector contained the numbers 2, 4, 6 and 8, and you are erasing 6, you just copy 2, 4 and 8 into a new array into indices 0, 1 and 3, while keeping slot 2 in the new array as undefined. Then you make a third array that is one shorter than the new array, and copy everything except the last item into the third array, including the undefined slot 2, but not the last slot (which should have been kept).

    I think that using temp[k] = v[i] would solve your problem in the erase function, but note that it still won’t be a perfect solution because the item being deleted might occur multiple times in the vector, or it might not be there at all, so it is not always the case that the new length is one smaller than the old length.

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

Sidebar

Related Questions

How smart is Garbage Collection when it comes to nested references? Take this code
I'm getting this warning (after use diagnostics;); Parsing of undecoded UTF-8 will give garbage
I keep getting this error after my code has run between 5-10 min CallbackOnCollectedDelegate
Can Some one help me the problem with this code? I am getting bunch
Can't seem to work out why I'm getting garbage output from this Linked List
Getting the above error in following code. How to rectify it. Thanks. Please look
Getting this error: 2009-09-03 12:44:02.307 xcodebuild[307:10b] warning: compiler 'com.apple.compilers.llvm.clang.1_0.analyzer' is based on missing compiler
Getting a rendering error for this form: 'NoneType' object has no attribute 'widget' http://dpaste.com/88585/
Getting this error with jquery & jquery.form. Site has been live for awhile..upgraded to
I'm attempting to simplify my code with a function that generates an array of

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.