Possible causes for strange vector display?

This is a discussion on Possible causes for strange vector display? within the C++ Programming forums, part of the General Programming Boards category; In my code I have a vector that should be a size of 1, containing the integer value 2. When ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Possible causes for strange vector display?

    In my code I have a vector that should be a size of 1, containing the integer value 2.

    When this portion of the code runs:
    Code:
    std::cout << "Root size: " << root->key.size() << "\n";
    
    typename std::vector<T>::iterator it = root->key.begin();
    for(; it != root->key.end(); ++it){
        std::cout << *it << " ";
    }
    and the output is as I expect:
    Code:
    Root size: 1
    2
    But when I have this in my code:
    Code:
    Node* curr = root;
    std::cout << curr->key[0] << "\n";
    std::cout << curr->key[1] << "\n";
    std::cout << curr->key[2] << "\n";
    This is the output:
    Code:
    2
    2
    0
    So what's with the extra "2" in the output?

    Is it because operator[] returns a reference and perhaps it's referencing some erroneous data? I'm confused because the size() is correct and the iterator display works correctly...

    I'm preplexed...

    BTW: replacing curr->key[x] with root->key[x] makes no difference, curr doesn't point to anything else in the function either.
    Last edited by dudeomanodude; 04-08-2008 at 11:10 AM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    The operator[] does not do any bounds checking, so by accessing elements at the indexes 1 and 2, you are invoking undefined behavior and probably just getting whatever random values are in memory at that location.

    The solution is to never pass operator[] anything outside of the range [0, size-1].

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just typical undefined behaviour - you are accessing a vector as an array, and accessing it out of bounds - if you use at() instead of operator[], you will get an exception thrown when you go out of bounds. The operator[] access method for std::vector is "undefined" for access beyond the size, and "anything" can happen.

    See: http://cpwiki.sourceforge.net/Undefined_behaviour

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Thanks guys.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

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