Thread: vector::size() unitialized at creation

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    vector::size() unitialized at creation

    Why is it that if you do not initialize a std::vector to 0 in the constructor that the size() function returns a huge number? It's obviously just returning unintialized data but I'm not sure why.

    std::map and other containers do not seem to suffer this. You can create a map and never insert anything and size() returns 0. Try this with vector and you will get some huge number for size. It's as if the implementors of the STL for Visual Studio did not init the size variable in the constructor for vector.

    Is this standard behavior for the STL or is this a bug in the MS version of STL?
    Last edited by VirtualAce; 03-07-2008 at 09:51 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I'd say that's a bug in the VS STL. I don't have immediate access to the C++ standard, but size() is supposed to return the number of elements in the vector, which is zero if none are explicitly initialized. Also, gcc-4.1.3 doesn't have this issue.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    could you post some code Bubba?

    I'm assuming your doing:

    Code:
    #include <vector>
    
    int main()
    {
    std::vector<int> foo;
    cout << foo.size();
    
    return 0;
    }
    Are we on the same page or are you talking about something different? I always get ZERO for the size.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I tried with VC++ 6.0 & 2005 and both return 0.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Interesting. Try it with user-defined data type pointers in the vector. I do not have MSVC 2003 at home so it would do me no good to test it here.
    Last edited by VirtualAce; 03-07-2008 at 11:47 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm pretty sure it works in general. Perhaps you're accessing a vector that isn't really constructed yet?

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    No the vector exists according to the debugger. I thought the same thing but if that were true calling size() on it should do far more than return a weird number. If the vector did not exist, calling size() on it would throw. The only way the vector cannot exist in this case is if the object in which it is being used somehow does not exist. It's very strange that the debugger steps through and there are no exceptions being thrown yet the vector size is completely inaccurate until at least 1 item is added. Can the compiler call a vector function and succeed even if the object using the vector has not been instantiated??

    Perhaps it is a side effect of problems in the code before it. I was sure that size() would return 0 even if the vector had not been init to 0. However I did not want to make this argument until I did some research. If this is indeed the case then this points to other issues.
    Last edited by VirtualAce; 03-08-2008 at 12:03 AM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If the vector did not exist, calling size() on it would throw.
    No it wouldn't. That would be undefined behavior, and since size is likely just a member variable I would expect it to return a weird number.

    >> Can the compiler call a vector function and succeed even if the object using the vector has not been instantiated??
    Yes. Or, if the vector was instantiated but the object was already destroyed.

    I've seen this behavior myself, and it is pretty much always that the instance of the class that contains the vector is not valid. The code will often still run but return weird results without any kind of access violation or exception.

    In the debugger, look at the other member variables of the instance that holds the vector (not the vector itself). Also look at the memory address of that object. Anything look funky? Any big numbers that should be small?

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I've seen this behavior before and your response was my first response to my co worker. The very odd thing is that the other member variables of the class that supposedly did not exist were completely valid. Pointers in the class were also valid. This may have led us down the wrong path since we assumed the instance was now valid due to what the debugger reported.

    Anyways I think I have enough evidence now to support the claim that even if you do not init a vector to 0 in an initializer list for a constructor or in the constructor itself size() will still return 0.

    Many thanks to those who tested this.
    Last edited by VirtualAce; 03-08-2008 at 12:38 AM.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The standard (refer table 65) also specifies that all default-constructed standard containers have size() equal to zero.

    If a vector of a particular user-defined type does not meet this requirement, then one thing to check is the implementation of that type. It is quite possible a little bit of undefined behaviour has crept in (eg a pointer molestation) and it just happens a visible effect is corruption of the vector into which the object is copied.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-16-2008, 02:43 PM
  2. handle creation of files
    By dathui in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2006, 01:16 AM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  4. Dynamic Creation of an object
    By axr0284 in forum Windows Programming
    Replies: 3
    Last Post: 02-05-2005, 10:27 AM
  5. Newton + Einstein were wrong!
    By Jez in forum A Brief History of Cprogramming.com
    Replies: 64
    Last Post: 12-14-2004, 02:24 PM