Hey,
I'm running into some issues with delete[]. I have these lines:
delete[] temp1;
delete[] temp2;
in a loop. temp1 and temp2 are pointers to std::strings. In the first iteration of the loop, temp1 and temp2 are both NULL pointers. The statements work fine. In the second iteration, the lines both still execute fine, but the the code immediately following them:
if(numAttributes > 1)
{
temp1 = new std::string[numAttributes-1];
temp2 = new std::string[numAttributes-1];
}
allocates arrays at temp1 and temp2.
By the third iteration, the two delete[] statements fail, and a dialog comes up saying Debug assertion failed. In the debugger, I saw that in the third iteration, temp1 and temp2 were labeled as "Bat Ptr" (which I'm assuming is bad pointer), rather than showing that they were NULL pointers ("0x000000" comes up).
Here is the bulk of the relevant code:
while(readAttributes)
{
numAttributes++;
if(!ReadAttribute(atr_name,atr_value,tagtype,readAttributes))
return 0;
delete[] temp1;
delete[] temp2;
if(numAttributes > 1)
{
temp1 = new std::string[numAttributes-1];
temp2 = new std::string[numAttributes-1];
}
for(int i = 0;i < numAttributes-1;i++)
{
temp1[i] = attributes[i];
temp2[i] = atrValues[i];
}
delete[] attributes;
delete[] atrValues;
attributes = new std::string[numAttributes];
atrValues = new std::string[numAttributes];
for(int i = 0;i < (numAttributes-1);i++)
{
attributes[i] = temp1[i];
atrValues[i] = temp2[i];
}
delete[] temp1;
delete[] temp2;
attributes[numAttributes-1] = atr_name;
atrValues[numAttributes-1] = atr_value;
}
So, what is the difference between NULL pointers and bad pointers, and why do the delete statements fail with the bad pointers?