メモリの割り当てと割り当て解除を処理する方法を学びたいです。私の最初の「タスク」は、少し体験することです。
new
基本的には、例外がスローされるまで、演算子を使用して 1 kB のメモリを割り当てたいと考えています。これを行う方法はよくわかりませんが、次のようなものになると思います。
int main(){
unsigned int counter = 0;
try{
for (int i = 0; i < 10; i++){
int *p_array = new int[1024*i];
cout << sizeof(p_array);
counter++
}
delete[] p_array;
}catch (std::bad_alloc& ba){
std::cerr << "bad_alloc caught: " << ba.what() << endl << "Allocated 1KB " << counter << " times";
}
return 0;
}
エラー:
test.cpp: In function ‘int main()’:
test.cpp:24:12: error: ‘p_array’ was not declared in this scope
make: *** [out_Executable] Error 1
非常に単純なプログラムですが、それでも行き詰まっています。誰か助けてくれませんか?