Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling bounds checking for c++ vectors

Tags:

c++

g++

libstdc++

With stl::vector:

vector<int> v(1);
v[0]=1; // No bounds checking
v.at(0)=1; // Bounds checking

Is there a way to disable bounds checking without having to rewrite all at() as []? I am using the GNU Standard C++ Library.

Edit: I changed at() to [] in the area where I suspected a bottleneck, and it significantly reduced the computation time. However, since I iterate between developing the code and running experiments with it, I would like to enable bounds checking during development and disable it when I run the experiments for real. I guess Andrew's advice is the best solution.

like image 736
matthiash Avatar asked Mar 05 '10 03:03

matthiash


1 Answers

If you really want to do it (at least for a quick and dirty profiling comparison), this will work if you have no other at()s

#define at(x) operator[](x)

And if you want to keep at() for development and use operator[] in production, just wrap it in an #ifdef.

And if you do have other at()s you can always edit your #included <vector> file.

like image 128
Andrew Stein Avatar answered Oct 04 '22 01:10

Andrew Stein