3 Replies - 478 Views - Last Post: 03 November 2010 - 12:33 PM Rate Topic: -----

#1 rmetzger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-October 09

Question about scope in functions?

Posted 03 November 2010 - 11:22 AM

Hello,

Lets say I have the following function:
void testFunction ()
{
  int i;
  Teapot teapot;
  Teapot * teapot;

  vector1.push_back (i);
  vector2.push_back (teapot);
  vector3.push_back (&teapot);
}


If each of those vectors is declared globally and holds the proper data type, will all of these vectors contain the data created in the function after the function executes?

I read that int i would be created on the stack and would only exist in the function scope. Does the same apply for Teapot teapot? If vector1 does contain int i after the function executes, how would the int be added to the vector if it is destroyed after the function executes?

I also read that Teapot * teapot is stored on the heap which is why vector3 will contain &teapot after execution. Do I have my heap and stack info correct?

Thanks!

This post has been edited by rmetzger: 03 November 2010 - 11:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about scope in functions?

#2 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Question about scope in functions?

Posted 03 November 2010 - 11:52 AM

vector::push_back creates copies of the passed object.
Your Teapot teapot gets destructed at the end of the function,
but the copy will remain in the vector.

Teapot* teapot; is not on the heap. It is just a pointer.
If you write teapot = new Teapot(); then you create a teapot
on the heap. This teapot will live until you delete it via
delete teapot; So be sure that you delete it before the pointer
to it get lost somehow. E.g. if you write vector3.empty() after
calling that function, the pointer to the teapot allocated on
the heap is lost, there is no way to use this memory space anymore.
This is called a Memory Leak.
Was This Post Helpful? 1
  • +
  • -

#3 rmetzger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-October 09

Re: Question about scope in functions?

Posted 03 November 2010 - 12:25 PM

View PostDjabby, on 03 November 2010 - 10:52 AM, said:

vector::push_back creates copies of the passed object.


So would it be more efficient to fill a vector with pointers to a dynamically allocated teapot instead of Teapot teapot; since the vector::push_back has to create copies of the object? If I create a dynamically allocated teapot using Teapot * teapot = new Teapot () in the function, when the function terminates would the teapot pointer be deleted leaving the teapot still on the heap?
Was This Post Helpful? 0
  • +
  • -

#4 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Question about scope in functions?

Posted 03 November 2010 - 12:33 PM

It is more efficient to use pointers. But the worst drawback is to be forced to handle all the allocating/freeing yourself. If you use vector<Teapot> you have the costs of a few more copies, but in charge, you don't have to think about freeing the allocated memory, vector does that job for you. So my advice: Use rather vector<Teapot> instead of vector<Teapot*>.
Or to be safe without any copying costs, you should google about "smart pointers". Especially boost's shared_ptr might be interesting. It would look like this then:

std::vector< boost::shared_ptr<Teapot> > vector2;
Teapot* myPot = new Teapot();
vector2.push_back(myPot);


shared_ptr cares about the memory freeing as soon as there is no reference/pointer to the myPot object.

This post has been edited by Djabby: 03 November 2010 - 12:38 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1