# Pointer to vector, scope and Locals

• 02-16-2010
Lagrange
Pointer to vector, scope and Locals
Hello fellow programmers,

I thought I had the basic stuff covered and started implementing an algorithm. Now the algorithm is finished and initial tests tells me that is works with respect to the solution it produces. Unfortunately it seems that the code does not scale with the problem instances I give it.

I have revised the method and it seems impossible that its the theory that is bugged. It _should_ scale reasonably. Hence, my money is on my poor implementation of the algorithm.

Looking at the Debug-> Windows -> Locals (Visual Studio 2008), I see a lot of variables I thought was deleted long ago. Also the size of them seems excessive. So I am thinking I have a memory leak. Well, leak might be a strong word, since I think the real problem is that I thought vectors from the STL behaved in a certain way.

The code it self is rather long (2000 lines +) so I will try to simplify my question regarding the vectors behavior.
Code:

```//A function definition before the main InitializeVector(vector<int>* pNumber) { vector<int> temp; //Do stuff with the vectors } VectorFun() { vector<int> NumberVector; vector<int>* pNumberVector; pNumberVector=& NumberVector; InitializeVector(pNumberVector); } main() { //Somewhere in the main function VectorFun();```
My question is now, will the vector temp be completly deleted after InitializeVector is run? Also will the NumberVector and pointer be deleted after the VectorFun?

Basically, will they remain ghosts in my memory or are they dead and gone?

Thank you in advance for time spendt on this *I think* easy question.

EDIT: Fixed the assignment of the pointer as stated in #2
• 02-16-2010
jeffcobb
Yes 'temp' should have destructed when the stack unwinds after InitializeVector() finishes. I say *should* as i have seen broken compilers where the dtor is not called when a variable falls out of scope. It has been a looong time since this was true however (think: early 90's).

I am curious: in what way could temp (even if it did "stick around" affect your algorithm? Also the point of VectorFun() seems...odd. You create a vector of ints, then a pointer of that type, never initialize it to anything but then pass that ptr to InitializeVector()?
• 02-16-2010
Lagrange
Ups, small mistake from my part

Code:

`pNumberVector = &NumberVector`

I am worrying that some improper cleanup due to poor programming is making my program very slow.

EDIT: Using watch I can see that vectors like temp is still present, even though I thought it should be out of scope. Where did I go wrong?
• 02-16-2010
iMalc
You're passing an uninitialised pointer into InitialiseVector. There is nothing useful that you can then do with the pNumber parameter inside InitialiseVector because it's not valid. Assigning to pNumber doesn't do anything useful either.
You should really show the real code, because I'd guarantee you've got bugs in this area.
• 02-17-2010
Bench82
Is there any reason for using a pointer over a reference? Doing so might make your program a little cleaner
Code:

```InitializeVector(vector<int>& pNumber) {     vector<int> temp;     //Do stuff with the vectors } VectorFun() {     vector<int> NumberVector;     InitializeVector(NumberVector); }```
If nothing else, it eliminates the possibility of your pointers causing you any problems.
• 02-18-2010
Lagrange
Bench82: As i understand doing that the Numbervector will be copied into the function and hence use more memory?

All: Alas, it turns out I have been looking the wronge places. I installed Ubuntu on my machine and, after minor changes to the code, g++'ed it. After running Valgrind it appears that there are no memory leaks in the program. After doing a Callgrind I found that the main reason for the poor performance was, Tadaaaa, a horrible implementation of a search called millions of times. The code is now able to run the big instances. I however need to find an improvement of factor 4, but I have some ideas.

Thanks to everybody for their time and effort!
• 02-18-2010
Bench82
Quote:

Originally Posted by Lagrange
Bench82: As i understand doing that the Numbervector will be copied into the function and hence use more memory?

No, look carefully - the Numbervector is a reference argument not a copy - a reference is just an alias for another object in memory. pass-by-reference is often used for referring objects which cannot be copied, such as I/O streams whose copy constructors and assignment operators are private.

In theory, passing by-reference could use very slightly less memory, since passing by-pointer involves a pointer being created on the stack. Whereas passing by-reference is simply extending the visible scope of your Numbervector to another function; in practise, it might be implemented as a pointer, but that's mostly irrelevent. references are nearly always safer than pointers, and never less safe.