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

  • Home
  • SEARCH
  • 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 7875771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:03:17+00:00 2026-06-03T03:03:17+00:00

I have a vector of pointers like so: vector<Item*> items; I want to clear

  • 0

I have a vector of pointers like so:

vector<Item*> items;

I want to clear it. I’ve tried:

for (unsigned int i = 0; i < items.size(); i++)
    delete items.at(i);
items.clear();

,

while (!items.empty())
{
    delete items.back();
    items.pop_back();
}

,

while (!items.empty())
{
    delete items.at(0);
    items.erase(items.begin());
}

, and

while (!items.empty())
    delete items.at(0);

Every single one of these blows up for some reason or another, including deletion of already deleted objects and out of range vector iterators.

What do I do? I want to be able to reuse that same vector and add more Item pointers into it later. Just using delete without clearing still leaves junk pointers in there, right?

EDIT: Okay, I’ve switched to shared_ptrs. Now I have

vector<shared_ptr<Item> > items;

Yet, when I do items.clear(); , I get the error “vector iterators incompatible”. What am I still doing wrong?

  • 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-03T03:03:19+00:00Added an answer on June 3, 2026 at 3:03 am

    Well, I did it. After a lot of time, a lot of aspirin, and a lot of lost hair, I finally figured out what the problem was. Turns out that I was calling a particular destructor earlier that contained the class which contained this vector of pointers. I had no idea that just calling a destructor would cause it to wipe all static data members. I hate c++ sometimes.

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

Sidebar

Related Questions

I have a vector of pointers. I would like to call a function for
I have a vector of pointers to objects. I'd like to remove objects from
I have a vector like so: vector<MyType*> _types; And I want to iterate over
I have something like this that is incorect: vector<boost::thread> vec; for(int agent = 1;
I have function that receives an array of pointers like so: void foo(int *ptrs[],
I have a vector of pointers to a class. I need to call their
I have a vector of pointers to Mouse objects called 'mice'. I'm passing the
I have a vector that holds pointers to abstract type Rock : vector<Rock*> rocks;
I have a vector of Linked list pointers. Each LinkedList has a head pointer
I have a vector for storing the pointers of all objects (created by new

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.