English 中文(简体)
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.

最佳回答

This

#include <iostream>
#include <vector>

int main()
{
    std::vector<unsigned __int32> v;
    v.reserve(2);
    std::cout << v.capacity() <<  
 ;
    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.

问题回答

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.

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

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





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签