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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:49:37+00:00 2026-05-31T02:49:37+00:00

I am working with a std::vector to hold some objects that have dynamically allocated

  • 0

I am working with a std::vector to hold some objects that have dynamically allocated members, and when I go to put the Things into the vector a few things happen that I am not understanding.

  1. I call push_back() and use the constructor of the objects as the argument, but for some reason it goes to the destructor of the object. why is this; it should be adding not deleting?

  2. I call push_back() a second time doing the same as before, but this time it throws an illegal memory access at dbgdel.cpp operator delete (line 52). but delete should never be called in a constructor, or push_back().

I am uncertain what sections of code are pertinent to this question as the lines in question are pretty entrenched in a method.

Edit: Code added

class Thing{
    int** Array;
    int size;   // of square array
    Point current; // location
    Thing(int _n){
        // allocates, and gives values to the array, and members
        // only constructor
    }
};
class ThingMgr{
    Thing * Control;
    Thing * Current;
    Thing * Previous;
    int size;  // size of all. same use as in Thing
    ThingMgr(int _n){
        size = _n;
        Control = new Thing(size);
        Current = new Thing(size);
        Previous = new Thing(size);
    }
    void rearrange(int _num){
        std::vector<Thing> possibles;

        // performs deterministic work on members

        // [0] first
        possibles.push_back(Thing(size)); // this succeeds
        // [1] second
        possibles.push_back(Thing(size)); // this fails

        // more operations to be performed never reached.
    }
};
  • 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-31T02:49:39+00:00Added an answer on May 31, 2026 at 2:49 am

    first: I call push_back() and use the constructor of the objects as the argument, but for some reason it goes to the destructor of the object. why is this; it should be adding not deleting?

    You are pushing a copy of that element into the vector. You construct a temporal element, its copy-constructor is called to create a copy within the vector, then the destructor of the temporal element is called.

    second: I call push_back() a second time doing the same as before, but this time it throws an illegal memory access at dbgdel.cpp operator delete (line 52). but delete should never be called in a constructor, or push_back().

    It’s strange that this happen on a second call, but eventually when the vector needs to regrow it copies elements again.

    You are probably failing to provide a proper copy-constructor for the element in question.

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

Sidebar

Related Questions

I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
I have a class that should hold a reference to some data, without owning
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
I am currently working on a project that deals with a vector of objects
I'm currently working on some logging code that supposed to - among other things
I'm working with a function which yields some data as a std::vector<char> and another
I've recently been working with code that looks like this: using namespace std; class
While working on this question , I noticed that GCC (v4.7)'s implementation of std::function
I found something surprising with std::vector that I thought I'd ask about here to
I have a table represented by a std::vector where every element is a boost::fusion::vector

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.