Question

all, finishing the semester and doing this student assignment with vector. I'm getting an intermittent bad_alloc error and a core dump during execution of the int remove() function. I believe it's happening when I use intList.pop_back(). It's my first time doing anything with a vector list and I'd be interested in learning why this is happening and how to do this properly to avoid this problem in the future. Any ideas will be greatly appreciated.

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout; 
using std::endl; 
using std::vector;

int remove(vector<int> *intList, int noOfElements, int removeItem);
void printResult(int removeItem, int removeReturn, int listLength);

int main()
{
    // variable declarations
    int removeReturn = 0;
    int removeItem = 0;
    unsigned int listLength = 0;
    unsigned int counter = 0;
    time_t seconds;
    vector<int> intList;

    // getting the time in seconds as a seed
    time(&seconds);
    // seeding the random number generator
    srand((unsigned int) seconds);
    // getting the random length for the calculations
    listLength = rand() % (100 - 1 + 1) + 1;
    std::cout << std::endl << "listLength is: " << listLength << std::endl;
    // populate the array with numbers from 1 to listLength
    std::cout << "Array:" << std::endl;
    for(counter = 0; counter < listLength; counter++)
    {
        intList.push_back(rand() % (listLength - 1 + 1) + 1);
        std::cout << intList.back() << " ";
        if(counter > 0 && (counter + 1) % 10 == 0)
            std::cout << std::endl;
    }
    std::cout << std::endl;
    listLength = intList.size();
    // generate a number in range to use for the search
    removeItem = rand() % (listLength - 1 + 1) + 1;
    // search for the number and delete it if found
    // changing the size of the vector array as you do
    std:: cout << "Searching for " << removeItem << " for removal." << std::endl;
    removeReturn = remove(&intList, listLength, removeItem);
    listLength = intList.size();
    // output the results of the operation
    printResult(removeItem, removeReturn, listLength);

    return 0;
} // end main

int remove(std::vector<int> *intList, int noOfElements, int removeItem)
{
    int counter;
    int icounter;

    // check to see if the vector list is empty
    // if it is, exit with -1 (error message)
    if(intList->empty())
        return -1;
    // assuming there is data in the vector list,
    // loop through and look for the element
    for(counter = 0; counter < noOfElements; counter++)
    {
        // if we do find an occurrence of the data in the vector list
        if(intList->at(counter) == removeItem)
        {
            // overwrite it with the next piece of data
            // and move the rest of the data up by one element
            for(icounter = counter; icounter < noOfElements - 1; icounter++)
            {
                    intList[icounter] = intList[icounter + 1];
            }
            // now that we have moved every data piece up one element
            // delete the last element from the list
            intList->pop_back();
            // return the position where the data was found
            return counter;
        }
    }
    // return 0 because we didn't find the data
    return 0;
} // end remove

void printResult(int removeItem, int removeReturn, int listLength)
{
    if(removeReturn > 0)
    {
        std::cout << "Found " << removeItem << " at position " << removeReturn +  << std::endl;
        std::cout << "and deleted it from the list " << std::endl;
        std::cout << "The new list length is " << listLength << std::endl;
    }
    else if(removeReturn == -1)
        std::cout << "The list was empty" << std::endl;
    else if(removeReturn == 0)
    {
        std::cout << "Value " << removeItem << " not found" << std::endl;
        exit(101);
    }
} // end printResult
Was it helpful?

Solution

If what you are trying to do is to search for an item in a std::vector and erase it, it doesn't take a complex function to do so:

#include <algorithm>
//...
intList.erase(std::remove(intList.begin(), intList.end(), removeItem), intList.end());

That one line does everything your entire remove function is attempting to do.

http://www.cplusplus.com/reference/algorithm/remove/ http://www.cplusplus.com/reference/vector/vector/erase/

In general, learn to use the STL algorithms. Moving, erasing, and ordering items in a container are operations that are routinely done in programs, therefore algorithms exist that perform these tasks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top