0

鉴于这段代码,它是更大代码的一部分,但这是罪魁祸首。

int main()我这样做:

int main(int argc, char* argv[]) {

// code 

// code 

path2 = (char*)malloc(strlen(currentDir)*sizeof(char));

traverseDirectories(path2 , filename);

free(path2);
}

错误 :

a@ubuntu:~/Desktop$ ./exer4 fsdfsdfdafsd/ exer4
Could not open: No such file or directory
*** glibc detected *** ./exer4: free(): invalid next size (normal): 0x08b54038 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ebc2)[0x93ebc2]
/lib/i386-linux-gnu/libc.so.6(+0x6f862)[0x93f862]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0x94294d]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0x92e0b4]
/lib/i386-linux-gnu/libc.so.6(perror+0xc5)[0x920a45]
./exer4[0x8048f52]
./exer4[0x8048b34]
./exer4[0x8048a99]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x8e9113]
./exer4[0x80487c1]
======= Memory map: ========
00398000-003b4000 r-xp 00000000 08:01 394171     /lib/i386-linux-gnu/libgcc_s.so.1
003b4000-003b5000 r--p 0001b000 08:01 394171     /lib/i386-linux-gnu/libgcc_s.so.1
003b5000-003b6000 rw-p 0001c000 08:01 394171     /lib/i386-linux-gnu/libgcc_s.so.1
008d0000-00a46000 r-xp 00000000 08:01 394150     /lib/i386-linux-gnu/libc-2.13.so
00a46000-00a48000 r--p 00176000 08:01 394150     /lib/i386-linux-gnu/libc-2.13.so
00a48000-00a49000 rw-p 00178000 08:01 394150     /lib/i386-linux-gnu/libc-2.13.so
00a49000-00a4c000 rw-p 00000000 00:00 0 
00b5a000-00b5b000 r-xp 00000000 00:00 0          [vdso]
00c4b000-00c69000 r-xp 00000000 08:01 394137     /lib/i386-linux-gnu/ld-2.13.so
00c69000-00c6a000 r--p 0001d000 08:01 394137     /lib/i386-linux-gnu/ld-2.13.so
00c6a000-00c6b000 rw-p 0001e000 08:01 394137     /lib/i386-linux-gnu/ld-2.13.so
08048000-0804a000 r-xp 00000000 08:01 947365     /home/a/Desktop/exer4
0804a000-0804b000 r--p 00001000 08:01 947365     /home/a/Desktop/exer4
0804b000-0804c000 rw-p 00002000 08:01 947365     /home/a/Desktop/exer4
08b54000-08b75000 rw-p 00000000 00:00 0          [heap]
b7600000-b7621000 rw-p 00000000 00:00 0 
b7621000-b7700000 ---p 00000000 00:00 0 
b775f000-b7760000 rw-p 00000000 00:00 0 
b776f000-b7771000 rw-p 00000000 00:00 0 
bf87d000-bf89e000 rw-p 00000000 00:00 0          [stack]
Aborted

以下是方法:

int traverseDirectories(char * path , const char * filename)
{

    // printf("\nCurrent path is :%s\n " , path);
    struct dirent *direntp = NULL;
    DIR *dirp = NULL;
    size_t path_len;
    char full_name[BUFFER];
    strcpy(full_name, path);

    displayAllFiles(full_name , filename);  // display the file details to STDOUT

// code 
// code 

}


void displayAllFiles(char * directory , char * filename)
{
    struct stat st;
    DIR *dir;
    struct dirent *ent;

    char fullPathDirectory[BUFFER];
    char fileDetails[BUFFER];

    memset(fullPathDirectory, '\0', BUFFER);   // reset the array of the input
    memset(fileDetails, '\0', BUFFER);   // reset the array of the input

    dir = opendir (directory);

    if (dir != NULL)
    {

         // print all the files and directories within directory
         while ((ent = readdir (dir)) != NULL)
         {
             if (strcmp(ent->d_name , filename) == 0)
             {
                  // printf("\nFile found!\n");

                  strcpy(fullPathDirectory , directory);
                  strcat(fullPathDirectory , "/");
                  strcat(fullPathDirectory , filename);

                  if (lstat(fullPathDirectory, &st) < 0)
                  {
                      perror(ent->d_name);
                      putchar('\n');
                      continue;
                  }

                  displayProperties(&st , fileDetails);

                  printf("\n%s" , fileDetails); // print file details
                  printf("%s\n" , fullPathDirectory); // print file path
             }

         }
         closedir (dir);
    }

    else
    {
     // could not open directory
     perror ("Could not open");
     free(directory);
     exit(EXIT_FAILURE);
    }

}

freedirectoryin 方法displayAllFiles,但即使我在执行时收到此消息:

./exer4 fsdfsdfdafsd/ exer4

程序没有找到文件夹fsdfsdfdafsd,这没问题,但为什么会打印所有的回溯?我想要它做的就是打印它no such file or directory

有任何想法吗 ?

谢谢

4

5 回答 5

4

一些快速的观察:

  1. 不要投射malloc().
  2. sizeof (char)是 1。
  3. 字符串需要为'\0'终止字符留出空间。
于 2012-06-19T10:02:12.243 回答
1

当来自. free(directory)_ displayAllFiles()_directorytraverseDirectories()

通常,您不应该exit()在那里调用,而只是返回一个错误指示器,调用者可以(并且应该)处理该错误。

于 2012-06-19T10:05:28.743 回答
1

这是您的代码的一部分。

char full_name[BUFFER];    
displayAllFiles(full_name , filename); 
.....
void displayAllFiles(char * directory , char * filename)
....
free(directory);

参数directory不是由 分配的malloc,不是由 分配free的地址malloc将导致未定义的行为。

于 2012-06-19T11:35:41.557 回答
1

我写了一个测试代码来模拟你的问题。

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 void problem(char * addr)
  5 {
  6     free(addr);
  7 }
  8 
  9 int func(char * buf)
 10 {
 11     char name[10];
 12 
 13 
 14 
 15     problem(name);
 16 
 17     return 0;
 18 }
 19 
 20 int main(void)
 21 {
 22     char *string;
 23 
 24     string = NULL;
 25     string = malloc(10);
 26     if(string == NULL)
 27         return -1;
 28 
 29     func(string);
 30     free(string);
 31     return 0;
 32 }
 33 

运行 valgrind 给出消息。

==17451== Invalid free() / delete / delete[] / realloc()
==17451==    at 0x59CFC: free (in /usr/local/lib/valgrind/vgpreload_memcheck-x86-freebsd.so)
==17451==    by 0x8048490: problem (main.c:6)
==17451==    by 0x80484B0: func (main.c:15)
==17451==    by 0x8048500: main (main.c:29)
==17451==  Address 0xbebfec2e is on thread 1's stack

您不能释放不是由 分配的地址malloc,这会导致问题。

于 2012-06-19T13:33:57.837 回答
0

问题在这里:

a@ubuntu:~/Desktop$ ./exer4 fsdfsdfdafsd/ exer4
Could not open: No such file or directory
*** glibc detected *** ./exer4: free(): invalid next size (normal): 0x08b54038 ***

问题是,当我分配这个时:

path2 = (char*)malloc(strlen(currentDir)*sizeof(char));

稍后更改path2指向 的字符串的大小,然后我尝试释放main 长度为 的字符串strlen(currentDir),然后得到以下信息:

 invalid next size (normal): 0x08b54038 ***

这意味着我尝试释放其他大小的内存,这当然是不正确的。这就是我这样做的原因 : path2 = malloc(BUFFER*sizeof(char));,一切正常。

谢谢大家 !

于 2012-06-19T13:38:26.060 回答