有没有办法逐个清除缓冲区?
我的代码中有一个 fread 行,它会像这样读取文件的内容
fread(buffer,sizeof(char),1024,file1);
我知道我可以使用 free(buffer);
释放缓冲区,但我的问题是,有没有办法删除缓冲区中的元素一一对应?比如说,我使用了第一个元素,但我不再需要它,所以我想删除它。 可能会这样做
for(i=0 ; i< 1024; i++){
*do something with buffer[1] *
free(buffer[1]); // I know this is wrong but something which can do the same.
}
谢谢, 苏尼尔
I've a fread line in my code that reads the contents of the file like this
fread(buffer,sizeof(char),1024,file1);
I kow that I can free the buffer using free(buffer);
but my questions is, is there a way to delete the elements in the buffer one by one?. Say, I used the first element and I no longer need it so I want to delete it.
Something which might do like this
for(i=0 ; i< 1024; i++){
*do something with buffer[1] *
free(buffer[1]); // I know this is wrong but something which can do the same.
}
Thanks,
sunil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,没有办法做到这一点。只需将原始指针存储在某处,然后增加您正在使用的指针即可。完成后,您可以随意
释放
原始指针。也就是说,如果您使用 C++,则根本不应该使用
malloc
或free
,也不应该使用fread
.. ..No, there is no way to do this. Just store the original pointer somewhere, and increment the pointer you're working with. When you're done, you can feel free to
free
the original pointer.That said, if you are using C++, you shouldn't be using
malloc
orfree
at all, nor should you be usingfread
....如果您想将分配/文件读取分解为较小的值,您可以这样做。不过,就像史蒂夫所说,您只能释放与您分配的完全相同的内存块。
更改现有代码的示例:
但是,您应该使用输入文件流而不是 fread。如果文件流要求您分配缓冲区(可能不会),请使用向量而不是 malloc/free。
If you want to break up your allocation/file read into smaller values, you could do it. Like Steve said, though, you can only deallocate the exact same chunks of memory you allocate.
An example of changing your existing code:
However, you should use an input file stream instead of fread. If file streams require you to allocate a buffer (they might not) use a vector instead of malloc/free.
您只能释放您分配的内容。您分配的是整个缓冲区,而不是缓冲区的各个元素。
来自N1256(我没有C99标准)
$7.20.3.2/2-“free函数导致ptr指向的空间被释放,也就是说,可供进一步分配。如果ptr是空指针,不会发生任何操作。否则,如果参数与 calloc、malloc 或 261) realloc 函数先前返回的指针不匹配,或者空间已通过调用 free 或 realloc 释放,则行为为然而,
如果您正在考虑删除从缓冲区读取的不必要的元素,则如果您使用数组,则必须自己执行此操作,否则您将必须适应 STL 容器(向量/列表)等(提供元素管理的接口)
You can free only what you allocated. What you allocated is the whole buffer and not the individual elements of the buffer.
From N1256 (I don't have the C99 standard with me)
$7.20.3.2/2- "The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or 261) realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
If you are however looking at removing unnecessary elements read from the buffer, you will have to either do it yourself if you are using arrays, or you will have to settle in for STL containers (vector/list) etc (which provide interfaces for element management)
执行此操作的 C++ stl 方法是
A C++ stl way of doing this would be
必须以与分配缓冲区相同的方式一次性释放缓冲区。您请求一个连续的内存块,该内存块会返回给您,并以描述缓冲区的堆控制信息作为开头。如果您尝试单独释放元素,堆管理器会感到困惑并且进程会出错。
The buffer has to be freed the same way it as allocated, in one shot. You asked for a contiguous block of memory, which is returned to you prefaced by heap control info describing the buffer as such. If you were to try to free elements individually the heap manager would get confused and the process fault.