views:

310

answers:

4

This works fine:

std::vector<int> v;  
v.push_back(123);

but this throws a std::length_error:

std::vector<uint32_t> v;// or vector<unsigned __int32>  
v.push_back(123);

It seems to be triggered by resizing, because

std::vector<uint32_t> v;  
v.reserve(2);

triggers a debug assertion "iterator not dereferencable".

This occurs on Visual Studio 2008, but the same code works fine on Mac and Linux. Can anyone suggest a way to narrow down the search for an explanation?



UPDATE: The rat's nest of static and dynamically linked dependencies in this project made it too time-consuming to find the offending library. I gave up and rebuilt every dependency from source. I lost two days of my life and still don't know exactly where the problem was, but the app runs! Thanks for your help.

+1  A: 

This

#include <iostream>
#include <vector>

int main()
{
    std::vector<unsigned __int32> v;
    v.reserve(2);
    std::cout << v.capacity() << '\n';
    return 0;
}

runs without any hiccups for me in VS 2008. It prints 2.

What does this do for you? If it works, too, then my first few guesses are:

  • You invoked undefined behavior somewhere before. By the time execution gets to the code you showed, all bets are off.
  • This is across DLL boundaries and you linked together DLLs/EXE built with different settings.

The way to find out about this is to distill it down to the smallest possible test case exhibiting the behavior. (That shouldn't contain more than 50LoC, ideally, it's 10.) If you don't find the problem while doing so, append the example to your question.

sbi
This is indeed the process I've been going through for several hours. I have an extremely complex project that exhibits the problem with my two-liner added to the top of main(), and I have a trivial project in which the same two lines run correctly, as several folks have noted here. I am gradually morphing the two toward each other in the hopes of finding the culprit.
maxfurni
@maxfurni: From my experience it seems more probable you'll find the problem by reducing the code that exhibits the problem than by expanding the other. `:)`
sbi
Note that for C++, the first line of main() is not necessarily the first line of your code that's being executed. Ctors of globals can run earlier.
MSalters
A: 

Since the sample code is working, your sample must be wrong :-)
Try to get closer to the problem by making your sample more like the real code in small steps. At some point it should stop working and then you can identify the culprit.

mxp
A: 

Can you check if your implementation has two or more typedefs of uint32_t? Especially under different namespaces? (I know the chances are pretty slim, but it might be worth it - in the quest for platform compatibility, different libraries try to map a specific memory size to a type, and one of them might have slipped up).

Fox
As noted in my code example, the behavior is the same with vector<unsigned __int32>
maxfurni
A: 

VC++ 2008 does not provide an ISO C99 header, so you must have provided the definion somehow; perhaps the definition is flawed.

Clifford
As noted in my code example, the behavior is the same with vector<unsigned __int32>
maxfurni