Error[8]: Undefined offset: 12, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我在Linux环境中使用C编程语言来读取目录中的文件.我包括#include< dirent.h>在我的代码中,我正在使用函数readdir().根据Linux在线页面,它说不会在结果指向dirent结构的地方调用free(),因为它可能被分配在堆栈上.你能帮我理解它是如何工作的吗?我不明白为什么我们不必删除struct dirent.什么时候删除谁删除它?

我在Linux环境中使用C编程语言来读取目录中的文件.我包括#include< dirent.h>在我的代码中,我正在使用函数readdir().

根据linux在线页面,它说不会在结果指向dirent结构的地方调用free(),因为它可能被分配在堆栈上.

你能帮我理解它是如何工作的吗?我不明白为什么我们不必删除struct dirent.什么时候删除谁删除它?

Here是我所说的摘录:

On success,readdir() returns a pointer to a dirent structure. (This structure may be statically allocated; do not attempt to free(3) it.) If the end of the directory stream is reached,NulL is returned and errno is not changed. If an error occurs,NulL is returned and errno is set appropriately.

最佳答案man readdir字面上说:

On success,readdir() returns a pointer to a dirent structure.
(This structure may be statically allocated; do not attempt to
free(3) it.)

(代码格式化程序已添加.)

这意味着它的空间不是在运行时分配的,例如堆栈或空闲存储内存,而是静态的:它在可执行文件本身,与字符串文字相当,区别在于写入字符串文字是未定义的行为.

想象一下实现是这样的:

struct dirent *readdir(DIR *dirp) {    static struct dirent dir;    /* Fill dir with appropriate values. */    return &dir;}

dir在这里静态分配.返回它的地址没有错,因为它存在于程序的整个运行时间中.

这是我的glibc 2.22实现上readdir的实际源代码(路径为/sysdeps/posix/readdir.c):

DIRENT_TYPE *__READDIR (DIR *dirp){  DIRENT_TYPE *dp;  int saved_errno = errno;#if IS_IN (libc)  __libc_lock_lock (dirp->lock);#endif  do    {      size_t reclen;      if (dirp->offset >= dirp->size)    {      /* We've emptIEd out our buffer.  Refill it.  */      size_t maxread;      ssize_t bytes;#ifndef _DIRENT_HAVE_D_RECLEN      /* Fixed-size struct; must read one at a time (see below).  */      maxread = sizeof *dp;#else      maxread = dirp->allocation;#endif      bytes = __GETDENTS (dirp->fd,dirp->data,maxread);      if (bytes <= 0)        {          /* On some systems getdents fails with ENOENT when the         open directory has been rmdir'd already.  POSIX.1         requires that we treat this condition like normal EOF.  */          if (bytes < 0 && errno == ENOENT)        bytes = 0;          /* Don't modifiy errno when reaching EOF.  */          if (bytes == 0)        __set_errno (saved_errno);          dp = NulL;          break;        }      dirp->size = (size_t) bytes;      /* reset the offset into the buffer.  */      dirp->offset = 0;    }      dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];#ifdef _DIRENT_HAVE_D_RECLEN      reclen = dp->d_reclen;#else      /* The only version of `struct dirent*' that lacks `d_reclen'     is fixed-size.  */      assert (sizeof dp->d_name > 1);      reclen = sizeof *dp;      /* The name is not terminated if it is the largest possible size.     Clobber the following byte to ensure proper null termination.  We     read Jst one entry at a time above so we kNow that byte will not     be used later.  */      dp->d_name[sizeof dp->d_name] = 'dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];';#endif      dirp->offset += reclen;#ifdef _DIRENT_HAVE_D_OFF      dirp->filepos = dp->d_off;#else      dirp->filepos += reclen;#endif      /* Skip deleted files.  */    } while (dp->d_ino == 0);#if IS_IN (libc)  __libc_lock_unlock (dirp->lock);#endif  return dp;}

我不太了解glibc但是线路

[+++]

对我们来说似乎是最有趣的. dirp->数据就是这里的静态数据,据我所知.

这就是为什么有可重入的替代readdir_r和readdir不可重入的原因.
想象两个线程同时执行readdir.两者都会尝试同时填充所有readdir调用之间共享的dir,导致无序的内存读/写. 总结

以上是内存溢出为你收集整理的从函数readdir释放(删除)已分配的内存全部内容,希望文章能够帮你解决从函数readdir释放(删除)已分配的内存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
从函数readdir释放(删除)已分配的内存_系统运维_内存溢出

从函数readdir释放(删除)已分配的内存

从函数readdir释放(删除)已分配的内存,第1张

概述我在Linux环境中使用C编程语言来读取目录中的文件.我包括#include< dirent.h>在我的代码中,我正在使用函数readdir().根据Linux在线页面,它说不会在结果指向dirent结构的地方调用free(),因为它可能被分配在堆栈上.你能帮我理解它是如何工作的吗?我不明白为什么我们不必删除struct dirent.什么时候删除谁删除它?

我在Linux环境中使用C编程语言来读取目录中的文件.我包括#include< dirent.h>在我的代码中,我正在使用函数readdir().

根据linux在线页面,它说不会在结果指向dirent结构的地方调用free(),因为它可能被分配在堆栈上.

你能帮我理解它是如何工作的吗?我不明白为什么我们不必删除struct dirent.什么时候删除谁删除它?

Here是我所说的摘录:

On success,readdir() returns a pointer to a dirent structure. (This structure may be statically allocated; do not attempt to free(3) it.) If the end of the directory stream is reached,NulL is returned and errno is not changed. If an error occurs,NulL is returned and errno is set appropriately.

最佳答案man readdir字面上说:

On success,readdir() returns a pointer to a dirent structure.
(This structure may be statically allocated; do not attempt to
free(3) it.)

(代码格式化程序已添加.)

这意味着它的空间不是在运行时分配的,例如堆栈或空闲存储内存,而是静态的:它在可执行文件本身,与字符串文字相当,区别在于写入字符串文字是未定义的行为.

想象一下实现是这样的:

struct dirent *readdir(DIR *dirp) {    static struct dirent dir;    /* Fill dir with appropriate values. */    return &dir;}

dir在这里静态分配.返回它的地址没有错,因为它存在于程序的整个运行时间中.

这是我的glibc 2.22实现上readdir的实际源代码(路径为/sysdeps/posix/readdir.c):

DIRENT_TYPE *__READDIR (DIR *dirp){  DIRENT_TYPE *dp;  int saved_errno = errno;#if IS_IN (libc)  __libc_lock_lock (dirp->lock);#endif  do    {      size_t reclen;      if (dirp->offset >= dirp->size)    {      /* We've emptIEd out our buffer.  Refill it.  */      size_t maxread;      ssize_t bytes;#ifndef _DIRENT_HAVE_D_RECLEN      /* Fixed-size struct; must read one at a time (see below).  */      maxread = sizeof *dp;#else      maxread = dirp->allocation;#endif      bytes = __GETDENTS (dirp->fd,dirp->data,maxread);      if (bytes <= 0)        {          /* On some systems getdents fails with ENOENT when the         open directory has been rmdir'd already.  POSIX.1         requires that we treat this condition like normal EOF.  */          if (bytes < 0 && errno == ENOENT)        bytes = 0;          /* Don't modifiy errno when reaching EOF.  */          if (bytes == 0)        __set_errno (saved_errno);          dp = NulL;          break;        }      dirp->size = (size_t) bytes;      /* reset the offset into the buffer.  */      dirp->offset = 0;    }      dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];#ifdef _DIRENT_HAVE_D_RECLEN      reclen = dp->d_reclen;#else      /* The only version of `struct dirent*' that lacks `d_reclen'     is fixed-size.  */      assert (sizeof dp->d_name > 1);      reclen = sizeof *dp;      /* The name is not terminated if it is the largest possible size.     Clobber the following byte to ensure proper null termination.  We     read Jst one entry at a time above so we kNow that byte will not     be used later.  */      dp->d_name[sizeof dp->d_name] = 'dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];';#endif      dirp->offset += reclen;#ifdef _DIRENT_HAVE_D_OFF      dirp->filepos = dp->d_off;#else      dirp->filepos += reclen;#endif      /* Skip deleted files.  */    } while (dp->d_ino == 0);#if IS_IN (libc)  __libc_lock_unlock (dirp->lock);#endif  return dp;}

我不太了解glibc但是线路

对我们来说似乎是最有趣的. dirp->数据就是这里的静态数据,据我所知.

这就是为什么有可重入的替代readdir_r和readdir不可重入的原因.
想象两个线程同时执行readdir.两者都会尝试同时填充所有readdir调用之间共享的dir,导致无序的内存读/写. 总结

以上是内存溢出为你收集整理的从函数readdir释放(删除)已分配的内存全部内容,希望文章能够帮你解决从函数readdir释放(删除)已分配的内存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1047311.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存