Warnings when using vector of vector

This is a discussion on Warnings when using vector of vector within the C++ Programming forums, part of the General Programming Boards category; I keep getting weird warnings. I have isolated the problem to the following test-case source file: Code: #include <vector> class ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Warnings when using vector of vector

    I keep getting weird warnings. I have isolated the problem to the following test-case source file:
    Code:
    #include <vector>
    
    class testclass
    {
    	public:
    		testclass(const testclass &o); // {};
    };
    
    void testfunc()
    {
    	std::vector <std::vector<testclass> > testobj;
    	testobj.push_back(std::vector<testclass>());
    }
    When compiled with
    g++ -W -O2 -c C:\Rotzooi\test.cpp -o obj\test.o
    I get the following errors:
    Code:
    stl_vector.h||In member function 
    	`std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) 
    	[with _Tp = testclass, _Alloc = std::allocator<testclass>]':|
    stl_vector.h|715|warning: '__result' might be used uninitialized in this function|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    stl_uninitialized.h||In member function 
    	`void std::vector<_Tp, _Alloc>::_M_insert_aux
    	(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) 
    	[with _Tp = std::vector<testclass, std::allocator<testclass> >, 
    	_Alloc = std::allocator<std::vector<testclass, std::allocator<testclass> > >]':|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    stl_uninitialized.h||In function `void testfunc()':|
    stl_uninitialized.h|82|warning: '__cur' might be used uninitialized in this function|
    (edited for readability)

    I do notice I don't get any warnings when I give the constructors function definition right in the class declaration. Also, no warnings without the optimalisation flag (and obviously not without the -W flag). Is this something to be worried about? And why do I get these warnings? Any way to prevent them?
    Thanks in advance. I'm really scratching my head at the moment.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    class testclass
    {
    	public:
    		testclass(const testclass &o); // {};
    };
    
    void testfunc()
    {
        //Vector of test_class vectors
        std::vector <std::vector<testclass> > main_vector;
    
        //Vector of test_class objects
        std::vector<testclass> testclass_vector;
                    
        //Create test_class object
        testclass test_object;
        
        //Add to test_class vector
        testclass_vector.push_back(test_object);
    
        //Add test_class vector to vector of test class vectors
        main_vector.push_back(testclass_vector);
    }
    This should fix it. My guess about the warnings is you did not actually have an instance of the vector you were attempting to add to your main vector. When I use complex containers such as this I will use typedef's to make the code easier to follow. Do you really need this complex container? Perhaps you could simplify your design so that you could also simplify your container. While I have used containers this complex I will say that they are confusing at times and usually unecessary. Another level of abstraction could remove the need for this type of container.
    Last edited by VirtualAce; 03-28-2008 at 10:09 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I see nothing wrong with the posted code. I'm guessing it's just a bug that occurs due to an optimization. A quick search of google brings up a similar thread where the same conclusion is reached.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Bubba: that code (after adding a default constructor) still generates the same warnings.
    Using a typedef or a wrapper class for std::vector<testclass> probably would be the right thing to do but I'd have to rewrite a bunch of code for that to work in what I'm working on. (also, when I tried it in my test code the typedef still generated the same warnings; a wrapper class also still leaves 3 of the 9 warnings)
    For now I'll just turn off optimisation and ignore the warnings if I ever need it back. (I really don't, right now)
    Thanks for the help guys!
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Well it is essentially the same thing. I was trying to get passed the optimizations that were flagging the error.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I tried it with my gcc 4.1.3 and 4.2.1 on Linux. No warnings at all.
    Code:
    cyberfish@cyberfish-laptop:/tmp$ g++ -Wall -O3 -c a.cpp
    cyberfish@cyberfish-laptop:/tmp$
    same with -O0 -O1 -O2.

    a.cpp
    Code:
    #include <vector>
    
    class testclass
    {
            public:
                    testclass(const testclass &o); // {};
    };
    
    void testfunc()
    {
            std::vector <std::vector<testclass> > testobj;
            testobj.push_back(std::vector<testclass>());
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 11:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 06:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

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