Alexander Lee
Alexander Lee

Reputation: 11

Strange behaviour with std::vector

consider this segment of codes:

std::vector<int> vecList;

...populate 3 elements into vecList...

if (!vecList.empty())
{
     std::cout << "List count = " << vecList.size() << std::endl;
     if (vecList.empty())
     {
          std::cout << "List is empty" << std::endl;
     }
}

my printout is:

List count = 3
List is empty

I did not do anything to "vecList" other than printing out, yet after I printed the size of the vector, the size becomes 0. How is that possible? Any advice is appreciated.

This happened when I am running my build on an Iphone environment.

Thanks Alexander

Upvotes: 1

Views: 1136

Answers (5)

swegi
swegi

Reputation: 4102

Looks your code like this? Note the semicolon after the second if.

std::vector<int> vecList;

...populate 3 elements into vecList...

if (!vecList.empty())
{
     std::cout << "List count = " << vecList.size() << std::endl;
     if (vecList.empty());
     {
          std::cout << "List is empty" << std::endl;
     }
}

Upvotes: 1

Robert S. Barnes
Robert S. Barnes

Reputation: 40558

You might also want to try Valgrind. Allot of times use of uninitialized values, especially with system calls can cause truly weird behavior.

For instance, a common mistake is the following ( yeah I've made this mistake myself ):

struct timeval tv;
tv.tv_sec = 5;

// This is supposed to sleep for 5 seconds
select(0, NULL,NULL,NULL, &tv);

What's missing? You need to init the second member of the struct as such tv.tv_usec = 0; otherwise it can cause seemingly random errors in completely unrelated sections of the program. Valgrind can help you catch some of these things.

Upvotes: 2

Narek
Narek

Reputation: 39871

Try debuging your code. It is easy and you will understand when exactly it becomes empty. Though, there is nothing ideal, anyway trust such constructs like STL and other well-kown libraries. In 99.99% cases the cause of the promblem is the programmer.

Upvotes: 1

sbi
sbi

Reputation: 224069

Since both std::vector<>empty() and std::vector<>::size() are const member functions and cannot alter the vector's content, the only ways I can see to get that result is by using multiple threads or invoking Undefined Behavior.

Likely candidates are other threads modifying the vector and messing up the vector's internals by buffer overflows and the like.

This

#include <iostream>
#include <vector>

int main ()
{
    std::vector<int> vecList;

    vecList.push_back(1);
    vecList.push_back(2);
    vecList.push_back(3);

    if (!vecList.empty())
    {
        std::cout << "List count = " << vecList.size() << std::endl;
        if (vecList.empty())
        {
            std::cout << "List is empty" << std::endl;
        }
    }

    return 0;
}

prints List count = 3 for me. I bet it does the same for you. If so, then there must be something messing things up in the code you don't show.

The only way to find out what it is (other than posting the exact right snippet here and have someone guess right) is to remove all extra code step by step until the problem disappears and then look at the code that triggered it.

Upvotes: 2

Viktor Sehr
Viktor Sehr

Reputation: 13099

If this actually happens, it's most probably another thread modifying the vector.

Upvotes: 0

Related Questions