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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:53:44+00:00 2026-06-08T10:53:44+00:00

I’m having trouble transferring some data contained in a vector between my functions. The

  • 0

I’m having trouble transferring some data contained in a vector between my functions. The situation is as follows:

void generateObjects(std::vector<MyClass> &objects)
{
    objects.clear();
    //Fill objects vector
    std::vector<MyClass> p;

    //This 4-line pattern is repeated a number of times to generate all objects and store them in variable 'objects'
    p.clear();
    generateSomeOfTheObjects(p); //p is again passed by ref. in/out parameter
    for(uint j = 0; j < p.size(); p++){
        objects.push_back(p[j]);
    }

    //Print some members of the objects - works fine
    for(uint i = 0; i < objects.size(); i++){
        printf("%f ",objects[i].mymember->myElm);
    }
}

int main()
{
   std::vector<MyClass> objects;
   generateObjects(objects);
   //Print size of vector - size is correct it is the same as it is in generateObjects func
   printf("%lu\n",objects.size());
   //Again print members of the objects - some members are retained after the function call, some are lost. 
   //The one below doesn't work, mymember is a pointer to another object and its member myElm seems not initialized.
   for(uint i = 0; i < objects.size(); i++){
       printf("%f ",objects[i].mymember->myElm);
   }
   //Here I need to pass the objects to another read-only function
   ...
}

I have searched the internet for similar cases and actually found many, but I couldn’t apply the same fixes to my code. I’m trying to reach a member of an object pointed to by a member of a MyClass instance (objects[i].mymember->myElm) What possibly am I missing here?

  • 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-08T10:53:45+00:00Added an answer on June 8, 2026 at 10:53 am

    Probably the error lies in the implementation of MyClass. I’d say that this class contains some pointer that is initialized with the address of a local variable, so when you return from some of the functions that pointer points to a destroyed object.

    That would undefined behavior but it may work by chance. When you return from the first function, the stack memory is finally overwritten and your data is lost.

    UPDATE: Thanks to the insight by @chris in the comments below, the most likely reason is that your MyClass does not have a copy constructor, but it does have a pointer member.

    Something like this:

    class MyClass
    {
    public:
        Member *mymember;
    
        MyClass()
        {
            mymember = new Member;
        }
        ~MyClass()
        {
            delete mymember;
        }
    };
    

    Now what happens if you use the compiler generated default copy constructor (or the copy operator)?

    void foo()
    {
        MyClass a;
        {
            MyClass b(a);
        }
        //a.mymember is no longer valid
    }
    

    Both a and b share the same pointer mymember, so when one of them is destroyed, the mymember is deleted and the other one holds a dangling pointer.

    That’s why we have the rule of three. It states:

    Whenever you define a non-default destructor, you most likely will want also a non-default copy-constructor and a non-default copy-operator.

    Now you have to decide if you want to share the ownership of the mymember or if you want to copy it. The first one is best done with smart pointers (shared_ptr) and the second one with deep copy.

    For example, deep copy:

    class MyClass
    {
    public:
        Member *mymember;
    
        MyClass()
        {
            mymember = new Member;
        }
        MyClass(const MyClass &c)
        {
            mymember = new Member(c.mymember);
        }
        MyClass &operator=(const MyClass &c)
        {
            if (this != &c) //be aware of self-copy
            {
                delete mymember;
                mymember = new Member(c.mymember);
            }
            return *this;
        }
        ~MyClass()
        {
            delete mymember;
        }
    };
    

    And with shared pointers:

    class MyClass
    {
    public:
        std::shared_ptr<Member> mymember; //or boost::shared_ptr if old compiler
    
        MyClass()
            :mymember(new Member)
        {
        }
        //no custom-made destructor -> no rule of 3
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.