关于c程序内存释放的两个小问题 (刚注册没分,请原谅)

hgalois 2007-07-21 04:36:10
下面是源程序 (两个问题我都写在里面相应的位置上了, 望高手指点):

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

/***** recursion directory manipulate ******
*author: hgalois
*last modify: 2007-7-21
*/


int recurDir(const char *path, int indent); /* recuring show dir items */

int main(int argc, char *argv[])
{
if(argc !=2)
{
printf("Syntax:%s path\n", argv[0]);
return 1;
}
recurDir(argv[1], 0);

return 0;
}

int recurDir(const char *path, int indent)
{
int i;
char tempStr[256], tempDname[256];
DIR *myDir;
struct dirent *myFile;
struct stat *fileStat;

fileStat = malloc(sizeof(struct stat));

if(!(myDir = opendir(path)))
{
perror("opendir()");
return -1;
}

while((myFile = readdir(myDir)) != NULL)
{
for(i = 0; i < indent; ++i)
putchar(' ');
printf("%s\n",myFile->d_name);

if(strncmp(myFile->d_name, ".", 1) && strncmp(myFile->d_name, "..", 2))
{
strcpy(tempStr, path);
strcat(tempStr, "/");
sprintf(tempDname, "%s", myFile->d_name);
strncat(tempStr, tempDname, 50);

if(lstat(tempStr,fileStat) == -1)
{
perror("lstat()");
return -1;
}
if(S_ISDIR(fileStat->st_mode) && !S_ISLNK(fileStat->st_mode))
recurDir(tempStr, indent + 4);
}
问题1:此处是否需要加上free(myFile)?我加上后会出现错误:(*** glibc detected *** free(): invalid pointer: 0x0804a084 ***
Aborted)
理论上我觉得应该加,因为指针再次赋值前一定先释放,否则会内存泄漏。
但是为什么会出错,请高手指点。
}

if(closedir(myDir) == -1)
{
perror("closedir()");
return -1;
}

free(fileStat);
问题2:此处是否需要加上free(myDir)?和问题1的出错方式一样。
return 0;
}

如果不加上这两条free语句程序运行正常,能得到预期结果。
...全文
201 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hgalois 2007-07-21
  • 打赏
  • 举报
回复
懂了,谢谢大家!
cceczjxy 2007-07-21
  • 打赏
  • 举报
回复
ls分析正确
mymtom 2007-07-21
  • 打赏
  • 举报
回复
man opendir 的部分输出:
====
DESCRIPTION
The opendir() function opens the directory named by filename, associates
a directory stream with it and returns a pointer to be used to identify
the directory stream in subsequent operations. The pointer NULL is
returned if filename cannot be accessed, or if it cannot malloc(3) enough
memory to hold the whole thing.

The readdir() function returns a pointer to the next directory entry. It
returns NULL upon reaching the end of the directory or detecting an
invalid seekdir() operation.

The readdir_r() function provides the same functionality as readdir(),
but the caller must provide a directory entry buffer to store the results
in. If the read succeeds, result is pointed at the entry; upon reaching
the end of the directory result is set to NULL. The readdir_r() function
returns 0 on success or an error number to indicate failure.
...
The closedir() function closes the named directory stream and frees the
structure associated with the dirp pointer, returning 0 on success. On
failure, -1 is returned and the global variable errno is set to indicate
the error.
...
Sample code which searches a directory for entry ``name'' is:

len = strlen(name);
dirp = opendir(".");
while ((dp = readdir(dirp)) != NULL)
if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
(void)closedir(dirp);
return FOUND;
}
(void)closedir(dirp);
return NOT_FOUND;
====
所以对于问题1,readdir, 由readdir_r的描述推测,readdir函数内部使用了静态变量,是不可重入的,所以不需要free
问题2的不用,因为closedir(myDir)已经完成这个工作了。
hgalois 2007-07-21
  • 打赏
  • 举报
回复
可不可以分析哈原因?谢谢!
dai_weitao 2007-07-21
  • 打赏
  • 举报
回复
你malloc什么就free什么。没有malloc的不用free。

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧