Pointer to vector, scope and Locals

This is a discussion on Pointer to vector, scope and Locals within the C++ Programming forums, part of the General Programming Boards category; Hello fellow programmers, I thought I had the basic stuff covered and started implementing an algorithm. Now the algorithm is ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    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
    Last edited by Lagrange; 02-17-2010 at 12:22 AM.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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()?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Ups, small mistake from my part

    Code:
    pNumberVector = &NumberVector
    Should have been added.

    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?
    Last edited by Lagrange; 02-16-2010 at 11:12 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    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.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    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!

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Lagrange View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Locals VS Globals (Mem Location)
    By csonx_p in forum Windows Programming
    Replies: 2
    Last Post: 06-25-2008, 02:35 AM
  2. Globals, locals and prototypes
    By face_master in forum C++ Programming
    Replies: 1
    Last Post: 09-01-2001, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21