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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:18:01+00:00 2026-05-19T10:18:01+00:00

I’m working my way through Accelerated C++ and have decided to mess around with

  • 0

I’m working my way through Accelerated C++ and have decided to mess around with the one of structs that were defined in there. While doing so, I’ve come across a problem: creating a vector of these structs and modifying the elements in each one seems to modify the elements in all of them.

I realize that this probably means I’ve initialized all the structs in the vector to a struct at a single memory address, but I used the .push_back() method to insert “dummy” structs in to the vector. I was under the impression that .push_back() pushes a copy of its argument, effectively creating a new struct.

Here is the header for the struct:

#ifndef _STUDENT_INFO__CHAPTER_9_H
#define _STUDENT_INFO__CHAPTER_9_H

#include <string>
#include <iostream>
#include <vector>

class Student_info9{
public:
    Student_info9(){homework = new std::vector<double>;};
    Student_info9(std::istream& is);

    std::string getName() const {return name;};
    double getMidterm() const {return midterm;};
    double getFinal() const {return final;};
    char getPassFail() const {return passFail;};

    std::vector<double> *getHw(){return homework;};

    void setName(std::string n) {name = n;};
    void setMidterm(double m) {midterm = m;};
    void setFinal(double f) {final = f;};


private:
    std::string name;
    double midterm;
    double final;
    char passFail;

    std::vector<double> *homework;
};


#endif  /* _STUDENT_INFO__CHAPTER_9_H */

And here is the code that i’m fooling around with (excuse the excessive print statements… the result of some time trying to debug 🙂 ):

vector<Student_info9> did9, didnt9;

bool did_all_hw9(Student_info9& s)
{
    vector<double>::const_iterator beginCpy = s.getHw()->begin();
    vector<double>::const_iterator endCpy = s.getHw()->end();
    return(find(beginCpy, endCpy, 0) == s.getHw()->end());
}

void fill_did_and_didnt9(vector<Student_info9> allRecords)
{
    vector<Student_info9>::iterator firstDidnt = partition(allRecords.begin(), allRecords.end(), did_all_hw9);


    vector<Student_info9> didcpy(allRecords.begin(), firstDidnt);


    did9 = didcpy;

    vector<Student_info9> didntcpy(firstDidnt, allRecords.end());
    didnt9 = didntcpy;


}

int main(int argc, char** argv) {

    vector<Student_info9> students;

    Student_info9 record;

    for(int i = 0; i < 5; i++)
    {
        students.push_back(record);
    }

    for(int i = 0; i < students.size(); i++)
    {
        students[i].setMidterm(85);
        students[i].setFinal(90);

        students[i].getHw()->push_back(90);
        std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;
        students[i].getHw()->push_back(80);
        std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;
        students[i].getHw()->push_back(70);
        std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;

        std::cout << "Just pushed back students[" << i << "]'s homework grades" << std::endl;

        if(i == 3)
            students[i].getHw()->push_back(0);
    }

    std::cout << "student[3]'s homework vector size is " << students[3].getHw()->size() << std::endl;

    for(vector<double>::const_iterator it = students[3].getHw()->begin(); it != students[3].getHw()->end(); it++)
        std::cout << *it << " ";

    std::cout << std::endl;

    std::cout << "students[3] has " << ( ( find(students[3].getHw()->begin(),students[3].getHw()->end(), 0) != students[3].getHw()->end()) ? "atleast one " : "no " )
            << "homework with a grade of 0" << std::endl;

    fill_did_and_didnt9(students);


    std::cout << "did9's size is: " << did9.size() << std::endl;
    std::cout << "didnt9's size is: " << didnt9.size() << std::endl;

}

As you can see by the print statements, it seems that the homework grades are being added only to one Student_info9 object, copies of which seem to be populating the entire vector. I was under the impression that if you were to use consecutive copies of .push_back() on a single object, it would create copies of that object, each with different memory addresses.

I’m not sure if that’s the source of the problem, but hopefully someone could point me in the right direction.

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-19T10:18:01+00:00Added an answer on May 19, 2026 at 10:18 am

    When you push a StudentInfo onto the vector, it is indeed copied, so that’s not the problem. The problem is the vector containing the homework grades. Since you only store a pointer to that vector in StudentInfo, only the pointer, not the vector, is copied when you copy a StudentInfo. In other words you have many different StudentInfos that all have a pointer to the same homework vector.

    To fix this you should define a copy constructor which takes care of copying the homework vector.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I know there's a lot of other questions out there that deal with this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.