有没有办法逐个清除缓冲区?

发布于 2024-09-16 05:11:34 字数 407 浏览 11 评论 0原文

我的代码中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

并安 2024-09-23 05:11:35

不,没有办法做到这一点。只需将原始指针存储在某处,然后增加您正在使用的指针即可。完成后,您可以随意释放原始指针。

也就是说,如果您使用 C++,则根本不应该使用 mallocfree,也不应该使用 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 or free at all, nor should you be using fread ....

孤凫 2024-09-23 05:11:35

如果您想将分配/文件读取分解为较小的值,您可以这样做。不过,就像史蒂夫所说,您只能释放与您分配的完全相同的内存块。

更改现有代码的示例:

const size_t size_of_full_buffer = 1024;
const size_t size_of_buffer_chunk = 128; /* evenly divisible */
size_t size_read = 0;

while(size_read <= size_of_full_buffer)
{
  buffer = (char*) malloc(sizeof(char) * size_of_buffer_chunk)
  fread(buffer, sizeof(char), size_of_buffer_chunk, file1);

  for(i = 0; i < size_of_buffer_chunk; i++)
  {
      /* do something with buffer[1] */
  }

  free(buffer);
  size_read += size_of_buffer_chunk;
}

但是,您应该使用输入文件流而不是 fread。如果文件流要求您分配缓冲区(可能不会),请使用向量而不是 malloc/free。

  • std::fstream、std::ifstream
  • std::vector

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:

const size_t size_of_full_buffer = 1024;
const size_t size_of_buffer_chunk = 128; /* evenly divisible */
size_t size_read = 0;

while(size_read <= size_of_full_buffer)
{
  buffer = (char*) malloc(sizeof(char) * size_of_buffer_chunk)
  fread(buffer, sizeof(char), size_of_buffer_chunk, file1);

  for(i = 0; i < size_of_buffer_chunk; i++)
  {
      /* do something with buffer[1] */
  }

  free(buffer);
  size_read += size_of_buffer_chunk;
}

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.

  • std::fstream, std::ifstream
  • std::vector
影子是时光的心 2024-09-23 05:11:35

您只能释放您分配的内容。您分配的是整个缓冲区,而不是缓冲区的各个元素。

来自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)

浮世清欢 2024-09-23 05:11:35

执行此操作的 C++ stl 方法是

#include <fstream>
#include <cassert>
#include <vector>

void foo () {
   const char* myFilename = "some_file_path"; 
   std::ifstream myStream (myFilename);

   assert (myStream.is_open());

   int n = 1024;
   std::vector<char> buffer (n);
   myStream.read (&buffer[0], n);
   assert (myStream.good());

   for (int i = 0; i < n; i++) {
       char* myPtr = &buffer[i];
       //do something with buffer[i]
   }

}

A C++ stl way of doing this would be

#include <fstream>
#include <cassert>
#include <vector>

void foo () {
   const char* myFilename = "some_file_path"; 
   std::ifstream myStream (myFilename);

   assert (myStream.is_open());

   int n = 1024;
   std::vector<char> buffer (n);
   myStream.read (&buffer[0], n);
   assert (myStream.good());

   for (int i = 0; i < n; i++) {
       char* myPtr = &buffer[i];
       //do something with buffer[i]
   }

}
栖竹 2024-09-23 05:11:34

必须以与分配缓冲区相同的方式一次性释放缓冲区。您请求一个连续的内存块,该内存块会返回给您,并以描述缓冲区的堆控制信息作为开头。如果您尝试单独释放元素,堆管理器会感到困惑并且进程会出错。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文