У меня вопрос о распределении и освобождении памяти.Использование strtof в цикле для разбора чисел из буфера символов
Я хочу прочитать буфер символов в цикле и сохранить значения float в вектор. Я получаю буфер, читая fstream.
Но мой подход всегда сбой при удалении буферов в конце.
Проблема в том, что я меняю буфер во время цикла? Есть ли у кого-нибудь идея, как это исправить?
Я благодарен за каждый намек!
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;
'станд :: копия (станд :: istream_iterator (FileStream), станд :: istream_iterator (), станд :: back_inserter (collectedDoubles));' –
Вам не нужно удалять Буфер2. Вы можете посмотреть здесь пример http://en.cppreference.com/w/cpp/string/byte/strtof – Shaana