maxfurni
maxfurni

Reputation: 339

strange std::vector problem with uint32_t on Visual Studio 2008

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.

Upvotes: 0

Views: 1914

Answers (4)

Clifford
Clifford

Reputation: 93554

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

Upvotes: 0

Fox
Fox

Reputation: 2138

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).

Upvotes: 0

sbi
sbi

Reputation: 224139

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.

Upvotes: 2

foraidt
foraidt

Reputation: 5709

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.

Upvotes: 0

Related Questions