简体   繁体   English

在发布模式下绑定检查迭代器(c ++)

[英]bound check for iterator in Release mode (c++)

I intentionally made iterator to exceed the size of std::vector like, 我有意让迭代器超过了std::vector的大小,就像

for (std::vector <Face>::iterator f = face.begin();
          f!=face.end()+5;
               ++f)
{
    // here I try to access (*f).P
    // note that I added 5 to the face.end()
}

I did not face any error neither during compiling and run-time. 在编译和运行时,我没有遇到任何错误。 How can I prevent that? 我怎么能防止这种情况?

If you want checked access to vector elements, you may use the at function which throws std::out_of_range exception in case of boundary violation. 如果要检查对vector元素的访问权限,可以使用at函数在发生边界违规时抛出std::out_of_range异常。 Example: 例:

for (std::vector<Face>::size_type i = 0;
          i != face.size()+5;
               ++i)
{
    face.at(i).f();
}

The standard doesn't specify any checked iterators. 该标准未指定任何已检查的迭代器。 The wording in the standard is that access to an invalid iterator results in undefined behavior. 标准中的措辞是访问无效迭代器会导致未定义的行为。 But many implementations provide checked iterators. 但是许多实现提供了经过检查的迭代器。 If portability is not a concern, you may use one of those checked iterators. 如果不考虑可移植性,则可以使用其中一个已检查的迭代器。 For example, in MSVC Debug mode, vector<T>::iterator is a checked iterator. 例如,在MSVC调试模式下, vector<T>::iterator是一个经过检查的迭代器。 In Release mode, however, it's just a typedef for T* 但是在Release模式下,它只是T*的typedef

What C++ compiler are you using? 你在用什么C ++编译器? VC10 (ie the C++ compiler in VS2010) in debug build correctly identifies the problem: 调试版本中的VC10(即VS2010中的C ++编译器)正确识别问题:

// Compile with:
// cl /EHsc /W4 /nologo /D_DEBUG /MDd test.cpp


#include <iostream>
#include <string>
#include <vector>


class Face
{
public:
    std::string Name;

    explicit Face(const std::string & name)
      : Name(name)
    {}
};


int main()
{
    std::vector<Face> faces;
    faces.push_back( Face("Connie") );
    faces.push_back( Face("John") );

    for (
        std::vector <Face>::iterator f = faces.begin();
        f != faces.end() + 5;
        ++f)
    {
        std::cout << f->Name << std::endl;
    }

    return 0;
}

When executing the resulting .exe, an error dialog box is showed with the following error message: 执行生成的.exe时,将显示一个错误对话框,并显示以下错误消息:

Expression: vector iterator + offset out of range 表达式:向量迭代器+偏移超出范围

For a release version of the standard C++ library you won't get build-in bounds checking on your iterators (or other operations which may be unchecked) because it would be comparatively slow. 对于标准C ++库的发行版,您不会在迭代器(或其他可能未选中的操作)上获得内置边界检查,因为它会相对较慢。 However, this doesn't mean that you need to compile in debug mode: as far as I understand the libstdc++ debug mode it can be used when compiling with compiler optimizations turned on. 但是,这并不意味着您需要在调试模式下进行编译:据我所知,libstdc ++调试模式可以在打开编译器优化时进行编译。 I would suspect that this true for other checked STL implementations because the checked code is typically just guided by setting up some macros appropriately. 我怀疑这对于其他已检查的STL实现是正确的,因为检查的代码通常只是通过适当地设置一些宏来指导。 To do this you'll need to find the appropriate macro settings and set them up appropriately with whatever you use to build your code. 为此,您需要找到适当的宏设置,并使用您用于构建代码的任何内容进行适当的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM