配列の要素を削除する際に問題があります。 以下のコードを見ることができます。C++プログラムchar配列の削除時に終了する
delete[] payload[x].data
が2回目に実行された後で、2番目のfor
ループが終了すると、プログラムは終了します。
struct s_payload{
u_char data[300];
}
.
.
u_char sample[300] ="Lorem ipsum dolor sit amet, eu mea laudem impetus.";
int _bufferSizeConnection = 1000;
int testCounter = 0;
s_payload* activeBuffer = new s_payload[_bufferSizeConnection];
for (;;) {
for (long int counter = 0; counter < _bufferSizeConnection; counter++) {
memcpy(activeBuffer[counter].data, sample, 299);
}
//_buffersConnection is std::queue
_buffersConnection.push(activeBuffer);
testCounter += 1;
if (testCounter == 15) {
LOG_INFO(">testcounter %d", testCounter);
break;
}
activeBuffer = new s_payload[_bufferSizeConnection];
}
for (int i = 0; i < 15; i++) {
//taking one of the activeBuffer from _buffersConnection
s_payload* payload = _buffersConnection.wait_and_pop();
for (int x = 0; x < _bufferSizeConnection; x++) {
u_char* current= payload[x].data;
delete[] payload[x].data; //Exit at the second running of this line;
}
delete[] payload;
}
このコードで何が問題になっていますか? ありがとうございます。
新しく電話をかけなかったものは削除できません。以下を参照してください。http://stackoverflow.com/questions/40134139/c-delete-a-pointer-free-memory – NathanOliver
struct s_payloadの@NathanOliverは固定サイズのchar配列を宣言しました。 –