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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:52:22+00:00 2026-05-22T22:52:22+00:00

#include <iostream> #include <vector> #include <cstdlib> #include <cassert> struct s_A { bool bin; s_A():

  • 0
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cassert>

struct s_A {
    bool bin;
    s_A(): bin(0) {}
};

class c_A {
public:
    s_A * p_struct;

    c_A(): p_struct(NULL) {p_struct = new s_A [16];}

    void Reset()
    {
        delete [] p_struct;
        p_struct = new s_A [16];
    }
};

int main () 
{   
    srand(1);
    int x = 30;
    std::vector <c_A> objects;
    objects.assign(x, c_A());
    std::vector <c_A> objects_copy;

    for(int q=0; q < x; q++)
    {
        objects_copy.push_back(objects[ rand() % x ]);
        objects_copy[q].Reset();
    }

    for(int q=0; q < 16; q++)
        for(int w=0; w < x; w++)
        {
            // Assertion should not fail, but it does
            assert(!objects_copy[w].p_struct[q].bin);
            objects_copy[w].p_struct[q].bin = true;
        }
}

Somehow the pointers in the different copied objects end up pointing to the same memory, and the assertion eventually fails. This behavior does not happen if run on the un-copied vector.I thought that c_A.Reset() should free up the pointers (via delete[]) to point to a new array, but I’m obviously missing something.

  • 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-22T22:52:23+00:00Added an answer on May 22, 2026 at 10:52 pm

    The particular source of your problem are these lines here:

    objects_copy.push_back(objects[ rand() % x ]);
    objects_copy[q].Reset();
    

    The issue is that when you try pushing copies of the objects into objects_copy, you end up making a shallow copy of the objects in the objects vector. This means that the objects in the two vectors will end up having pointers that are copies of one another. Consequently, when you invoke Reset on the elements of the objects_copy vector, you deallocate memory that is still being pointed at by by elements of the objects array.

    The problem is that your c_A class violates the rule of three. Because your class encapsulates a resource, it needs to have a destructor, copy constructor, and copy assignment operator. If you define these three functions, then when you try copying the objects into the objects_copy vector, you will have the ability to manage the underlying resource, perhaps by duplication or by reference-counting. For details on how to write these functions, check out this description of how to write these functions.

    EDIT: Here’s a more detailed description of what’s going on:

    The issue is that when you add an object to a vector, you aren’t actually storing that object in the vector. Rather, you’re storing a copy of that object. Thus when you write objects_copy.push_back(objects[ rand() % x ]);, you are not storing the same object in both vectors. Instead, you’re creating a copy of one of the objects from objects and storing it in objects_copy. Since your c_A type does not have copy functions defined, this ends up making a shallow-copy of the object, which creates a copy of the pointer. This means that if you think about the original object from the objects list and its corresponding copy in objects_copy, they will each have a copy of the same p_struct pointer. When you call Reset on the object in the objects_copy vector, you free the memory pointed at by its pointer. However, you did not update the pointer of the original object stored in objects, and so that pointer now refers to garbage memory. Trying to use that pointer leads to undefined behavior, hence the crash.

    Adding copy functions will fix this by allowing you to control how the copy is being made. If you define a copy function for c_A that causes the copy to point to a new copy of the object pointed at by the original, then this problem won’t occur because each object will have its own separate pointer. Alternatively, if you use reference counting, then you can avoid the problem by not deleting the resource if you know that some other object points at it.

    Hope this helps!

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

Sidebar

Related Questions

#include iostream #include vector class ABC { private: bool m_b; public: ABC() : m_b(false)
#include <iostream> #include <vector> #include <cassert> class a_class { public: int num_IN; a_class():num_IN(0){} a_class(a_class
#include iostream #include vector class ABC { }; class VecTest { std::vector<ABC> vec; public:
#include <iostream> #include <vector> int main() { class Int { public: Int(int _i) :
Consider the following code : #include <vector> #include <iostream> class a { public: int
#include<iostream> #include<vector> #include<algorithm> class Integer { public: int m; Integer(int a):m(a){}; }; class CompareParts
#include <iostream> #include <vector> using namespace std; typedef struct Record { std::string name; bool
#include <iostream> #include <vector> using namespace std; class Base { public: void Display( void
#include <iostream> #include <vector> using namespace std; class Parent { public: Parent(); void method();
Consider the following code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm>

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.