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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:46:07+00:00 2026-06-01T08:46:07+00:00

I am working on a fairly large C++ project which unfortunately doesn’t really use

  • 0

I am working on a fairly large C++ project which unfortunately doesn’t really use C++ to its full potential. Large portions of the code are still plain C wrapped in ridiculous C++ classes.

So I tried to make the code more readable and more safe by introducing some C++ and STL.

However, when I assign a vector to an object member I get a strange crash (debug error to be exact). Imagine this:

class A
{

public:

    // Default constructor
    A()
    {
        initialize(std::vector<unsigned long>());
    };

    A(const std::vector<unsigned long> &data)
    {
        initialize(data)
    };

    ~A() {};

    void initialize(const std::vector<unsigned long> &data)
    {
        m_data = data;
    };

private:

    std::vector<unsigned long> m_data;

};

Then somewhere else in the code, I call:

a.initialize(std::vector<unsigned long>());

However, the program terminates du to a debug error: vector iterators incompatible.
It happens on this assignment:

m_data = data;

… which should make a copy, which is what I intend to do.

However, if I change this line to:

m_data = std::vector<unsigned long>(data);

… everything works as expected.

My questions are: Why’s that? And is this the correct way to initialize an object with an empty vector?

Keep in mind that the class in reality is much bigger and has much more members which are passed, which is why I use an initialize function. And there is also the possibility of passing an already existing array, so I do need to be able to initialize it either empty or with existing data.

EDIT: I have found the cause of the crash. I have found out that the code I’m looking at actually works a bit different:

A *pA = malloc(...); // Not really malloc but a wrapper for a WINAPI-Alloc
// Some stuff happens in between
pA->initialize(std::vector<unsigned long>());

So in fact the constructor of A has never been called and therefore the constructor of the vector member (m_data) also never executeded. That’s why assigning a constructor worked while implicitly calling the copy-function crashed, since the destination vector hasn’t been constructed yet. Well a lesson learned and more horrible code to improve 🙂

Thanks!

  • 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-01T08:46:09+00:00Added an answer on June 1, 2026 at 8:46 am

    I don’t know why your code crashes with that error, as it looks ok, superficially. Perhaps you should edit your question to include a minimal test case.

    However, there are better ways to do initialisation:

    class A
    {
    public:
    
        // Default constructor.
        // Relies on the fact that the default constructor for std::vector is
        // an empty vector.
        A()
        {}
    
        // Use the initialisation list.
        A(const std::vector<unsigned long> &data)
        : m_data(data)
        {}
    
    private:
        std::vector<unsigned long> m_data;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a fairly large aspx web project with extensive use of
I'm working on a fairly large project for a trading company in Philadelphia. The
I'm working on a fairly large project in Python that requires one of the
I am working in Java on a fairly large project. My question is about
I am working on a fairly large project that runs on embedded systems. I
I'm working on a fairly large project at the moment and am currently in
I'm working on a fairly large C++ project on Linux. We are trying to
I'm working on a fairly large PHP project written in a procedural style (it
I am working on a fairly large project and have recently bumped into the
I've been working on a fairly large C++ project that surprisingly uses MS Access

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.