1

I the below code , could you please tell if the scope of point object in function getObjects() is valid or not. I am creating object in createObj() , so scope of object "p" is limited to createObj()....does vector take care of this using copy constructor ?

void getObjects()
{
    vector<point> vec;
    creatObj(vec);
    // getting correct output for object allocated in vector vec.
}

void createObj(vector<point> &vec)
{
    point p;
    p.x=10;
    p.y=20;
    vec.push_back(p);
}
Matthieu Harlé
  • 739
  • 1
  • 13
  • 32
user1057741
  • 265
  • 1
  • 2
  • 10

2 Answers2

4

It's all good, as the push_back call in createObj creates a copy.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    I was looking at:http://www.cplusplus.com/reference/vector/vector/push_back/. The site mentions "The content of val is copied (or moved) to the new element". What does `move` mean here. I understand the value "copied" or "moved" are not semantically same. – Aman Deep Gautam Jul 07 '13 at 12:32
  • If it moves, then all pointers attached to it before becomes invalid? – huseyin tugrul buyukisik Jul 07 '13 at 12:37
  • @AmanDeepGautam See [What are move semantics?](http://stackoverflow.com/q/3106110/395760) –  Jul 07 '13 at 12:47
0

Since you're not passing a pointer into the vector, you're safe. If you were passing in a pointer, you would need to ensure that what the pointer points to remains valid as long as you needed it.

mah
  • 39,056
  • 9
  • 76
  • 93
  • you mean to say if i allocate point object memory in heap and store the pointer in vector object. i guess it still be valid , if i don't delete objects allocated in the vector ?? – user1057741 Jul 07 '13 at 12:39
  • Yes, your objects in the heap will have their contents preserved after the function call terminates, and the pointers to them (which you copied into your vector) are still valid pointers because of this (until you delete the objects, of course). – mah Jul 07 '13 at 12:40
  • even if delete say object x (which takes input point *p=new point() )...... now after delete if i iterate the vector ..will it cause me any trouble???... if yes then how can i avoid it – user1057741 Jul 07 '13 at 12:42
  • Iterating the contents of the vector will not directly be a problem, but using the pointers that are no longer valid will!). To avoid this, you might want to never directly allocate your new objects, but instead provide your own functions... one to "allocate and push to vector", and another to "remove from vector and delete". If you don't/can't manage like this, then you must be very careful. Some language/runtimes (like ObjectiveC) make this easier for you, but C++ requires you to take more care. – mah Jul 07 '13 at 12:45
  • @Mah: Nope, C++ does it for you too. Use a smart pointer, not `new`/`delete`. – Lightness Races in Orbit Jul 07 '13 at 14:14
  • @mah : could you please provide an example or tutorial link for the method you have suggested ? – user1057741 Jul 07 '13 at 17:03
  • I don't know of a particular tutorial but I would suggest you google for "smart pointer c++" to follow up with @LightnessRacesinOrbit 's suggestion. – mah Jul 07 '13 at 17:19
  • No, do not "google for tutorials". Obtain a quality, peer-reviewed C++ book. http://stackoverflow.com/q/388242/560648 – Lightness Races in Orbit Jul 07 '13 at 19:04