大佬教程收集整理的这篇文章主要介绍了c – 在GCC的“Debug”版本中绑定检查std :: array,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
检查范围违规特别是对于多维数组可能是有益的,因为在这种情况下,范围违规不太可能导致segfault(因为你经常拥有内部数组的内存所以[5000] [ – 123]仍然可能指向内存你拥有的).
const uint32_t dim1=10*1000,dim2=3; std::array<std::array<int,dim2>,dim1> test_2Darray; int undefined_value=test_2DarraY[dim2-1][dim1-1]; std::cout<<"ouch ("<<undefined_value<<")"<<std::endl; int ok_value=test_2DarraY[dim1-1][dim2-1]; std::cout<<"OK ("<<ok_value<<")"<<std::endl; // test_2Darray.at(dim2-1).at(dim1-1); -->terminate called after throwing an instance of 'std::out_of_range' // what(): array::at
如果你问为什么我不切换到.at() – 我可能需要性能,我也有很多代码[]已经写好了,我不够聪明,不能聪明地替换1D更不用说2D了阵列.
我使用GCC 4.6
#include <array> #include <Cassert> #include <iostream> #ifndef NDEBUG template <typename T,std::size_t N> struct my_array : std::array<T,N> { T& operator[](std::size_t n) { assert(n < N); return (*static_cast<std::array<T,N>*>(this))[n]; } const T& operator[](std::size_t n) const { assert(n < N); return (*static_cast<const std::array<T,N>*>(this))[n]; } }; #else // I would use Alias templates here,but isn't supported on my compiler yet! template <typename T,N> { }; #endif
它与std :: array不完全匹配,但如果对你很重要,可以修复它.然后用my_array替换对std :: array的所有引用,你将获得调试版本的范围检查operator [].
(我已经使用模板别名来简化NDEBUG代码,但我还不能在我的编译器上测试它)
以上是大佬教程为你收集整理的c – 在GCC的“Debug”版本中绑定检查std :: array全部内容,希望文章能够帮你解决c – 在GCC的“Debug”版本中绑定检查std :: array所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。