tags:

views:

152

answers:

5

Hi, 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

A: 

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

Viktor Sehr
+2  A: 

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.

sbi
Hmm... Is unrestricted concurrent access also qualified as undefined behavior?
sharptooth
@sharptooth: Since the std doesn't even acknowledge the existence of threads... But thanks anyway, I'll clarify.
sbi
Hi all, thanks for your feedbacks. I have considered other threads running concurrently. But my code is similar to sbi. It works sometime, but there are just some freak moments where "List is empty" is printed. My codes run on top of an engine which I does not have the source code for now, though I doubt that multi-threading is the issues. In any case, thanks a lot!
Alexander Lee
If it happens only some of the time, then it does sound like a threading problem. (Or maybe it's failing hardware.)
jamesdlin
+1  A: 

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.

Narek
+2  A: 

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.

Robert S. Barnes
+1  A: 

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;
     }
}
swegi