Thread: vector<float*> push_back() problem

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Question vector<float*> push_back() problem

    Hello,

    I have a problem with the vector<vector<float*>>. Here is the code:
    Code:
    	std::vector<float*> one_vector;
    	for(int i=0; i< 49; i++)
    	{
    		float one_float[3]={(float)i, (float)i, (float)i};
    		one_vector.push_back(one_float);
    	}
    With breadpoint, I saw that, after each push_back, one_vector changes unexpectly.
    when i=0, one_vector={0.0};
    when i=1, one_vector={1.0, 1.0};
    when i=2, one_vector={2.0,2.0, 2.0};
    ...

    Have no idea, why the old elements are all changed after push_back().
    Does anyone know what's going on there?
    Thanks in advance.
    Last edited by stella1016; 09-17-2009 at 02:24 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are storing pointers to a local array that goes out of scope after each iteration (the pointer becomes invalid almost immediately) . Essentially you are storing a pointer to the same memory location over and over, overwriting the contents of that memory each time.

    It is somewhat hard to tell from this example what you want to achieve.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    Quote Originally Posted by anon View Post
    You are storing pointers to a local array that goes out of scope after each iteration (the pointer becomes invalid almost immediately) . Essentially you are storing a pointer to the same memory location over and over, overwriting the contents of that memory each time.

    It is somewhat hard to tell from this example what you want to achieve.

    Thanks for your reply.
    Now I see my problems.

    Yes, I should initialize the float* at first, otherwise it is overwritten.

    I changed it to:
    Code:
    	
            std::vector<float*> one_vector;
    	for(int i=0; i< 49; i++)
    	{
    		float *one_float=new float[3];
                    one_float[0] =(float)i, one_float[1]=(float)i, one_float[2]=(float)i;
    		one_vector.push_back(one_float);
    	}
    It works fine. The only thing not good is that, I have to free the space afterwards. So I make it another way now. Since I only have fix-sized float*, I can use struct instead:
    std::vector<my_struct>
    ...

    Now I am happy with it. Thanks for your help.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Indeed, to store a fixed-sized array in a vector, make it copyable by wrapping it up in a struct. To maximize reusability, you can make the struct a template:

    Code:
    #include <vector>
    
    template <class T, unsigned Size>
    struct ArrayWrapper
    {
        T array[Size];
    };
    
    int main()
    {
        std::vector<ArrayWrapper<float, 3> > one_vector;
        for(int i=0; i< 49; i++) {
            ArrayWrapper<float, 3> arr = {{ float(i), float(i + 1), float(i + 2) }};
            one_vector.push_back(arr);
        }
    }
    In fact you might find a more convenient version of the above in boost: Boost.Array. You may also check if your SC++L implements tr1 (planned library additions in C++0x).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM