make_checked_array_iterator
他のアルゴリズムで使用できる checked_array_iterator を作成します。
注意
この関数は、標準 C++ ライブラリの Microsoft 拡張機能です。この関数を使用して実装されるコードは、Microsoft 拡張機能をサポートしない C++ 標準ビルド環境には移植できません。
template <class Iter>
checked_array_iterator<Iter>
make_checked_array_iterator(
Iter Ptr,
size_t Size,
size_t Index = 0
);
パラメーター
Ptr
コピー先配列へのポインター。Size
ターゲット配列のサイズ。Index
配列のインデックス (省略可能)。
戻り値
checked_array_iterator のインスタンス。
解説
make_checked_array_iterator 関数は stdext 名前空間で定義されています。
この関数は、生のポインター (通常、境界オーバーランに関する問題の原因となる) を受け取り、チェックを行う checked_array_iterator クラスでポインターをラップします。 そのクラスはチェック済みとしてマークされるので、STL はそれについて警告を表示しません。 詳細およびコード例については、「チェックを行う反復子」を参照してください。
使用例
次の例では、ベクターが作成され、10 個の項目が設定されます。 ベクターのコンテンツはコピー アルゴリズムで配列にコピーされ、make_checked_array_iterator を使用してコピー先が指定されます。 その後、境界チェックの意図的な違反によって、デバッグのアサーション エラーがトリガーされます。
// make_checked_array_iterator.cpp
// compile with: /EHsc /W4 /MTd
#include <algorithm>
#include <iterator> // stdext::make_checked_array_iterator
#include <memory> // std::make_unique
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template <typename C> void print(const string& s, const C& c) {
cout << s;
for (const auto& e : c) {
cout << e << " ";
}
cout << endl;
}
int main()
{
const size_t dest_size = 10;
// Old-school but not exception safe, favor make_unique<int[]>
// int* dest = new int[dest_size];
unique_ptr<int[]> updest = make_unique<int[]>(dest_size);
int* dest = updest.get(); // get a raw pointer for the demo
vector<int> v;
for (int i = 0; i < dest_size; ++i) {
v.push_back(i);
}
print("vector v: ", v);
copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));
cout << "int array dest: ";
for (int i = 0; i < dest_size; ++i) {
cout << dest[i] << " ";
}
cout << endl;
// Add another element to the vector to force an overrun.
v.push_back(10);
// The next line causes a debug assertion when it executes.
copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));
}
出力
vector v: 0 1 2 3 4 5 6 7 8 9
int array dest: 0 1 2 3 4 5 6 7 8 9
必要条件
ヘッダー:<iterator>
名前空間: stdext