2

Coming from a Java background, following code is confusing me. Confusion here is C++ object creation without new keyword. I am creating this object Student_info info; and then adding it to a vector. Since I am not creating it using new keyword, will it be allocated on stack and be destroyed after the loop exits? If that is the case, how the last loop is able to extract the info from vector correctly? Shouldn't it throw exception as all the object added to the vector has been destroyed?

struct Student_info {
    string name;
    vector<double> marks;
};

int main()
{
    int num = 2;

    vector<Student_info> student_infos;
    for (int i = 0; i < 3; i++) {
        Student_info info; //will this object be destroyed once this loop exits?
        cout << "Enter name:";
        cin >> info.name;

        double x;
        cout << "Enter 2 marks";
        for (int j = 0; j < num; j++) {
            cin >> x;
            info.marks.push_back(x);
        }
        student_infos.push_back(info);
    }

    for (int i = 0; i < 3; i++) {
        cout << student_infos[i].name;
        for (int j = 0; j < num; j++) {
            cout << student_infos[i].marks[j];
        }
        cout << endl;
    }
    return 0;
}
1
  • Since you are coming from a Java background I really advise you to learn the difference between a variable and a pointer! Also keep in mind that whenever you pass something as an parameter it gets copied. You get the Java behaviour when you use pointers instead. Then you also have to use the new keyword. Commented Mar 22, 2016 at 12:12

4 Answers 4

3

You are correct in that the info object will be destroyed, but it is copied into the vector when using push_back. The rest of the code reads the copy from the vector and not the original object.

http://www.cplusplus.com/reference/vector/vector/push_back/

The copying is done using the object's copy constructor which in this case is compiler-provided. It will in turn call the copy constructors on the string and vector members of the struct.

Sign up to request clarification or add additional context in comments.

Comments

3

The scope of variable info is the block scope of the for statement starting from the point of declaration of the variable.

for (int i = 0; i < 3; i++) {
    Student_info info; //will this object be destroyed once this loop exits?
    //...
}

Each time a new iteration of the loop is executed a new object with name info is created.

The scope of variable student_infos is the outer most block scope of function main where the variable is declared.

int main()
{
    vector<Student_info> student_infos;
    //...
}

When the loop is executed a new object is added to the vector that is a copy of variable info. The vector does not contain a reference to the original object with name info. It creates a copy of the object and stores it within itself internally.

Take into account that in general these loops

for (int i = 0; i < 3; i++) {
    cout << student_infos[i].name;
    for (int j = 0; j < num; j++) {
        cout << student_infos[i].marks[j];
    }
    cout << endl;
}

are unsafe. It is much better to rely on the actual number of elements in the vectors. You could write instead

for ( vector<Student_info>::size_type i = 0; i < student_infos.size(); i++)
{
    cout << student_infos[i].name;
    for ( vector<double>::size_type j = 0; j < student_infos[i].marks.size(); j++) 
    {
        cout << student_infos[i].marks[j];
    }
    cout << endl;
}

Comments

2

Yes, info will be destroyed at the end of the loop. push_back creates a new copy from info, so even if info is destroyed, its copy still exist in the vector.

Comments

0

It will copy the temp Student_info info to the student_infos when push_back is done. If you can write a custom copy constructor for Student_info and print something there you will see the copy construct will invoked. Consider using, http://en.cppreference.com/w/cpp/container/vector/emplace_back

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.