English 中文(简体)
C++ 在矢量存取中未定义的行为
原标题:C++ Undefined behaviour in vector access
Following up the p0532r0.pdf documentation the following piece of code results in UB #include #include struct X { const int i; X(int _i) : i(_i) {} friend std::ostream& operator<< (std::ostream& os, const X& x) { return os << x.i; }; }; int main(){ std::vector v; v.push_back(X(42)); v.clear(); v.push_back(X(77)); std::cout << v[0]; // undefined behaviour } However no UB is detected when the above piece of code is run with sanitiser. demo Why does compiler not detect this error? Apart from that based on the P0532R0, when type of vector is a class that has a const member , the internal usage of placement new causes UB. How is this issue overcome ?
问题回答
Firstly, this behaviour is undefined by the standard, but is implicitly well-defined on GCC. From your linked PDF: AFAIK, gcc currently does not optimize the constant/reference case, but does optimizations for the vptr part. So in a sense there s nothing to detect. Secondly, the reason this UB is not detected by ubsan: it doesn t check for it at all in the first place. The manual (for 9.5) lists the behaviours checked by ubsan here, and links the equivalent clang ubsan documentation. Neither claim to detect the particular un-laundered access to a placement-new overwritten object with a const member that you re concerned with. BTW, Your demo is using GCC 9.1. None of this changes with later versions up to and including trunk AFAICS, but that s why I linked the 9.5 manual.




相关问题
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?

热门标签