文字列のベクトルからc文字列の配列を作成しています。結果の配列がベクトルの最初の要素をスキップするようにします。これに使用している関数は次のとおりです。
char** vectortoarray(vector<string> &thestrings)
{
//create a dynamic array of c strings
char** temp = new char*[thestrings.size()-2];
for(int i = 1; i < thestrings.size(); i++)
temp[i-1] = (char*)thestrings[i].c_str();
return temp;
}
小さなプログラムでエラーなしでテストしたので、このコードが機能することはわかっています。ただし、わずかに大きいプログラム内で実行すると、エラーが発生しますterminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
。
これを防ぐにはどうすればよいですか?