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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:15:14+00:00 2026-05-26T14:15:14+00:00

I have a vector declared as: vector<Curve> workingSet; Curve is a class I’ve created

  • 0

I have a vector declared as:

vector<Curve> workingSet;

Curve is a class I’ve created that contains a string “name” and an array of structs, dynamically allocated by the constructor.

I have a loop that is supposed to delete 2 (out of 4) items from the vector.

vector<Curve>::iterator it = workingSet.begin();

    //remove tbi's children from the working set
    for (  ;  it != workingSet.end();){
        if (it->thisName == tbi.childAName ||it->thisName == tbi.childBName)
            it= workingSet.erase(it);
        else
            ++it;
        }

When the debugger reaches the .erase(it) call I can see that the ‘it’ iterator is pointing to curve 2 in the vector. This is good; I want curve 2 to be removed from the vector.

I’m then taken by the debugger to the destructor (I have a breakpoint there), which presumably should be destructing curve 2. But when I look at the ‘this’ watch, I can see that the curve being destructed is curve 4! The destructor then proceeds to ‘delete[]’ the array in the object, as required and sets the array pointer to NULL.

When the debugger returns to the program, having completed the erase() call, I can see that vector 2 has been removed from the array and curve 4 is still there. Curve 4’s array pointer still points to the same location as before, but the memory has been deallocated and the array contains garbage.

Can anyone suggest why curve 4 is being messed with?

N.b. (1) There is a copy constructor for the curve class, which does a ‘deep’ copy.
N.b. (2) There is more to the class/program than I’ve mentioned here

Btw, the array pointers in curves 2 and 4 point to different locations and contain different values, according to the debugger.

Edit: I’ve now implemented copy assignment. Now the correct item seems to be being erased from the vector, but the wrong destructor is still being called! However, when the debugger returns to the array curve 4 is still intact.

  • 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-05-26T14:15:14+00:00Added an answer on May 26, 2026 at 2:15 pm

    When an item is erased from the vector, all elements behind it are shifted towards the front to fill the empty space. If your compiler doesn’t support move yet, it’s done by copying all the elements, and the last item in the vector, which is now copied to the item before it, is a duplicate and is getting deleted.

    At least that’s how it should work.

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

Sidebar

Related Questions

I have a vector-like class that contains an array of objects of type T
I have a vector declared as a global variable that I need to be
I have a vector t_vec that stores references to instances of class Too .
I have a vector of A objects. class A contains a member object of
I have structs, let's call them sn, that look like: struct sn { string
Say I have a simple struct that contains a vector and defines a copy
So I have declared a vector in my class header like this: ... private:
I have two vectors declared as a private class property: private Vector<Myobject> v1 =
I have a vector that I want to insert into a set . This
I have a vector of beans that holds information I want to display in

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.