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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:27:52+00:00 2026-05-28T16:27:52+00:00

I have a problem in a piece of code I’m writing. I’ve boiled it

  • 0

I have a problem in a piece of code I’m writing. I’ve boiled it down to this example which produces the same results (in principal).

Basically I’ve got a vector of pointers to Objects that I come across in one class, and pass to another. I then notice that by going through this vector with an iterator I come across NULL or garbage values, whereas using a traditional for-loop runthrough everything’s fine.

Here’s the code:

#include <iostream>
#include <vector>

using namespace std;

class MyObject
{
  public:
    int a;
    int b;

    MyObject(int _a, int _b)
    {
      a = _a;  b = _b;
    }
};

class Builder
{
  public:
    Builder() {
      obj_vector.push_back(new MyObject(1, 2));
      obj_vector.push_back(new MyObject(3, 4));     
    }

    vector<MyObject*> getObjectVector() { return obj_vector; }

  private:
     vector<MyObject*> obj_vector;  
};

class MyObjectHolder
{
  public:
    MyObjectHolder()
    {
      Builder builder;
      obj_vector = builder.getObjectVector();
    }

    vector<MyObject*> getObjectVector() { return obj_vector; }

  private:
    vector<MyObject*> obj_vector;

};

int main()
{
  MyObjectHolder holder;

    for (vector<MyObject*>::iterator itr = holder.getObjectVector().begin(); itr != holder.getObjectVector().end(); ++itr)
    {   
      if ((*itr) == NULL) {
        cout << "NULL" << endl;         
      }
      else
      {
        cout << (*itr)->a << "\t" << (*itr)->b << endl;
      } 
    }   
    cout << endl;

    for (unsigned int i = 0; i < holder.getObjectVector().size(); i++)
    {       
      if (holder.getObjectVector()[i] == NULL)
        cout << "NULL" << endl;
      else
        cout << holder.getObjectVector()[i]->a << "\t" << holder.getObjectVector()[i]->b << endl;
    }

    return 0;
}

And the output of this program is:

0   0
3   4

1   2
3   4

So I’m wondering why the iterator object produces garbage values. In other instances the iterator does equal NULL. I’m guessing it has something to do with some of the bbjects going out of scope while being passed around between objects, but I can’t figure out where or why.

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-05-28T16:27:53+00:00Added an answer on May 28, 2026 at 4:27 pm

    holder.getObjectVector() returns a new vector. So holder.getObjectVector().begin() and holder.getObjectVector.end() are unrelated, as exists in for loop.

    Changing getObjectVector() to return a reference would correct this:

    vector<MyObject*>& getObjectVector() { return obj_vector; }
    

    EDIT:

    An alternative would be to arrange to call getObjectVector() once before the for loop and store the result and it would not be necessary to return a reference:

    vector<MyObject*> objs = holder.getObjectVector();
    for (vector<MyObject*>::iterator itr = objs.begin(); itr != objs.end(); ++itr)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've wrote this simple piece of code. And I have a slight problem with
I have problem with this piece of code: var el = $('div#editor'); el.find('*[contentEditable]').removeAttr('contentEditable'); It
I have a problem with this piece of code when I build it for
I have a problem with this piece of code If I don't use the
I have a problem with this piece of code in C. #include <stdio.h> #include
Currently learning Scheme/Racket and have problem running this piece of code. (if (or (<
i have this problem from mathematica,which refers to ising model.Here is a piece of
I have a problem with content_tag into an helper, this piece of code def
hello everyone I have some problem with understanding this piece of the code: import
I have a problem with this piece of code : var logo = document.getElementById(move_this);

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.