I have a question about allocating and releasing memory.
I want to read a char buffer in a loop and save the float values ββto a vector. I get the buffer by reading an fstream.
But my approach always crashes when deleting the buffers at the end.
Is it a problem that I change the buffer during my loop? Has anybody an idea how to fix this?
I am thankful for every hint!
char* buffer1 = new char[size]; // the size is given
char* buffer2 = NULL;
fileStream.read(buffer1,size);
while(true)
{
// read double from buffer
// and get pointer to the new buffer -> buffer2
double tempDouble = strtod(buffer1, &buffer2);
// If nothing has been read (buffer stays the same) -> break
if (buffer1 == buffer2)
break;
else // change the buffer to read the next double during the next interation step
buffer1= buffer2;
// collect the doubles
collectedDoubles.push_back(tempDouble);
// if all numbers are read -> break
if (++doubleCount == numDoubles) // termination condition
break;
}
// delete the allocated buffer
delete[] buffer1;
// do I have th delete the 2nd buffer too?
// delete[] buffer2;