在循环中使用 strtof 解析字符缓冲区中的数字
Using strtof in a loop to parse numbers from a char buffer
我有一个关于分配和释放内存的问题。
我想在循环中读取一个char缓冲区并将float值保存到一个vector中。
我通过读取 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;
根据strtod
的文档:
The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is NULL, it is ignored.
所以你的指针 buffer2
仍然是 NULL
并且在你执行 buffer1= buffer2;
- buffer1
现在也是 NULL
(顺便说一句,这里是内存泄漏因为数据丢失了)。
do I have th delete the 2nd buffer too?
在这种情况下 - 不,因为删除 NULL
指针是 no-operation.
解法:
看一下文档中为strtod
函数提供的示例,根据您的代码,这里是类似的:
char* buffer1 = new char[size];
char* buffer2; // note no NULL here !
char* p = buffer1; // we'll modify this in loop
for (double tempDouble = std::strtod(p, &buffer2); p != buffer2; tempDouble = std::strtod(p, &buffer2))
{
p = buffer2;
if (errno == ERANGE){ // check if some error occured during attempt to convertion
std::cout << "range error\n";
errno = 0;
}
collectedDoubles.push_back(tempDouble);
if (++doubleCount == numDoubles) // termination condition
break;
}
delete[] buffer1;
编辑 1: 看看@JerryCoffin 在对您的问题的评论中建议的优雅且非常 'C++ like' 的解决方案。
我有一个关于分配和释放内存的问题。
我想在循环中读取一个char缓冲区并将float值保存到一个vector中。 我通过读取 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;
根据
strtod
的文档:The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is NULL, it is ignored.
所以你的指针
buffer2
仍然是NULL
并且在你执行buffer1= buffer2;
-buffer1
现在也是NULL
(顺便说一句,这里是内存泄漏因为数据丢失了)。do I have th delete the 2nd buffer too?
在这种情况下 - 不,因为删除
NULL
指针是 no-operation.
解法:
看一下文档中为strtod
函数提供的示例,根据您的代码,这里是类似的:
char* buffer1 = new char[size];
char* buffer2; // note no NULL here !
char* p = buffer1; // we'll modify this in loop
for (double tempDouble = std::strtod(p, &buffer2); p != buffer2; tempDouble = std::strtod(p, &buffer2))
{
p = buffer2;
if (errno == ERANGE){ // check if some error occured during attempt to convertion
std::cout << "range error\n";
errno = 0;
}
collectedDoubles.push_back(tempDouble);
if (++doubleCount == numDoubles) // termination condition
break;
}
delete[] buffer1;
编辑 1: 看看@JerryCoffin 在对您的问题的评论中建议的优雅且非常 'C++ like' 的解决方案。