stl::vector problems (memory?)

This is a discussion on stl::vector problems (memory?) within the C++ Programming forums, part of the General Programming Boards category; Hello, I am experimenting with the stl::vector again, and I have this class: PHP Code: struct test {     int Number ; ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    stl::vector problems (memory?)

    Hello, I am experimenting with the stl::vector again, and I have this class:

    PHP Code:

    struct test
    {
       
    int Number;
    };

    class 
    CTest
    {
       public:
       
       
    void Fill();
       
    void Test();

       
    vector<test*> vec;
    }; 
    now here is the code:
    PHP Code:
    void CTest::Fill()
    {
       
    //I push one structure.
       
    test t;

       
    t.Number 5;
       
    vec.push_back(&t);
    }

    void CTest::Test()
    {
       
    char str[20];
       
    sprintf (str"%i"vec[0]->Number);
       
    MessageBox(0str""0);

    Looks as it works? No, it doesnt, the MessageBox shows that Number is garbage and NOT 5 as expected. However, if I test this inside the Fill function (the exact code), it shows the real value assigned to Number!

    Oh, I use pointer in the vector, as I might change the Number later, I want it to be updated in the vector.

    What is happening here?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    copy in and copy out is the STL way and you have fallen foul of it.
    You only store pointers in your vector, well copies of pointers anyway and you have added to the vector a copy of a pointer that points to a local object on the stack that has since been destroyed. Use dynamic allocation instead and dont forget to delete the newed memory before destroying the container.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >What is happening here?
    In CTest::fill(), t is a local variable. Since you push its address onto the vector, as soon as the function returns, the memory used by t is reclaimed for other uses, so the pointer in the vector points to memory you no longer own. Try dynamically allocating memory to the vector, or don't use pointers
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <vector>
    
    using namespace std;
    
    struct test
    {
        int Number;
    
        test(int num): Number(num) {}
    };
    
    class CTest
    {
    public:
        ~CTest();
        void Fill();
        void Test();
        vector<test*> vec;
    };
    
    CTest::~CTest()
    {
        // You must delete new'd memory, always
        vector<test*>::iterator it;
    
        for (it = vec.begin(); it != vec.end(); ++it)
            delete *it;
    }
    
    void CTest::Fill()
    {
        vec.push_back(new test(5));
    }
    
    void CTest::Test()
    {
        char str[20];
        sprintf (str, "%i", vec[0]->Number);
        MessageBox(0, str, "", 0);
    }
    
    int main()
    {
        CTest t;
    
        t.Fill();
        t.Test();
    }
    p.s. What the alphabet would look like without q and r.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Oh but of course

    Thanks a lot!!
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. available memory from task manager
    By George2 in forum Tech Board
    Replies: 10
    Last Post: 01-18-2008, 02:32 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM

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