1

我没有使用Dynamic Memory Allocation,也没有在任何地方释放内存。它甚至给出了双重免费错误。有人可以帮我吗?

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <limits.h>

#define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
#define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
#define EVENT_SIZE  ( sizeof (struct inotify_event) ) /*size of one event*/
#define BUF_LEN     ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
#define SIZE 20000
int main( int argc, char **argv ) 
{
    int length, i = 0, wd;
    int fd;
    char buffer[BUF_LEN];

    time_t timer;
    char buffer2[25];
    char *s=NULL;
    struct tm* tm_info;

    FILE *fk;
    //char buffer[EVENT_BUF_LEN];
    char str1[] = "File has been updated at time :";
    time(&timer);
    tm_info = localtime(&timer);
    strftime(buffer2, 25, "%Y:%m:%d %H:%M:%S", tm_info);
    fk = fopen( "/home/technoworld/Desktop/file.txt" , "w" );

    /* Initialize Inotify*/
    fd = inotify_init();
    if ( fd < 0 ) {
        perror( "Couldn't initialize inotify");
    }

    /* add watch to starting directory */
    wd = inotify_add_watch(fd, argv[1], IN_MODIFY ); 

    if (wd == -1)
    {
        printf("Couldn't add listen to %s\n",argv[1]);
    }
    else
    {
        printf("Listening: %s\n",argv[1]);
    }

    /* do it forever*/
    while(1)
    {
        i = 0;
        length = read( fd, buffer, BUF_LEN );  

        if ( length < 0 ) {
            perror( "read" );
        }  

        while ( i < length ) {
            struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
            if ( event->len ) {

                if ( event->mask & IN_MODIFY) {
                    if (event->mask & IN_ISDIR)
                        printf( "The directory %s was modified.\n", event->name );       
                    else
                    {
                        printf( "The file %s was modified at %s \n", event->name, buffer2 ); 
                        fprintf(fk,"%s %s %c",str1, buffer2, '\n');
                    }
                }

                i += EVENT_SIZE + event->len;
            }
        }
        fcloseall(fk);
    }

    /* Clean up*/
    inotify_rm_watch( fd, wd );
    close( fd );

    return 0;
}

我正在尝试侦听文件中的更改并在日志文件中写入带有时间戳的更改。

错误:

Listening: /home/technoworld/Desktop/tmp/
The file .goutputstream-L3JTXW was modified at 2013:05:24 22:06:21 
The file .goutputstream-IRR4XW was modified at 2013:05:24 22:06:21 
*** glibc detected *** ./a: double free or corruption (fasttop): 0x09438008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7624ee2]
./a[0x80488bb]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75c84d3]
./a[0x80485c1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 07:00 558766     /home/technoworld/Desktop/a
08049000-0804a000 r--p 00000000 07:00 558766     /home/technoworld/Desktop/a
0804a000-0804b000 rw-p 00001000 07:00 558766     /home/technoworld/Desktop/a
09438000-09459000 rw-p 00000000 00:00 0          [heap]
b7578000-b7594000 r-xp 00000000 07:00 394123     /lib/i386-linux-gnu/libgcc_s.so.1
b7594000-b7595000 r--p 0001b000 07:00 394123     /lib/i386-linux-gnu/libgcc_s.so.1
b7595000-b7596000 rw-p 0001c000 07:00 394123     /lib/i386-linux-gnu/libgcc_s.so.1
b75ae000-b75af000 rw-p 00000000 00:00 0 
b75af000-b7752000 r-xp 00000000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7752000-b7753000 ---p 001a3000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7753000-b7755000 r--p 001a3000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7755000-b7756000 rw-p 001a5000 07:00 394098     /lib/i386-linux-gnu/libc-2.15.so
b7756000-b7759000 rw-p 00000000 00:00 0 
b776e000-b7773000 rw-p 00000000 00:00 0 
b7773000-b7774000 r-xp 00000000 00:00 0          [vdso]
b7774000-b7794000 r-xp 00000000 07:00 394076     /lib/i386-linux-gnu/ld-2.15.so
b7794000-b7795000 r--p 0001f000 07:00 394076     /lib/i386-linux-gnu/ld-2.15.so
b7795000-b7796000 rw-p 00020000 07:00 394076     /lib/i386-linux-gnu/ld-2.15.so
bfe5d000-bfe7e000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)
4

3 回答 3

1

fcloseall不接受任何论据。(这甚至是如何编译的?)

即使初始fopen成功(所以fk不是NULL),在第一次while (1)循环之后,您将调用fcloseall并关闭文件。因此,fprintftofk正在打印到一个关闭的文件。由于文件操作分配和使用内存,并且关闭操作释放了该内存,因此您正在使用释放的内存,因此出现 glibc 错误。

于 2013-05-25T05:38:38.570 回答
0

问题就在那里,因为您正在使用 64 位系统并且您不包括在内time.h,并且 localtime(&timer) 的返回类型被解释为 32 位整数而不是 64 位指针(未声明的类型默认为 int)。 ..您正在丢失该隐式声明中的信息。

石蕊测试,看看我是否正确:

根据包括声明以下权利:

#include <limits.h>
struct tm;
struct timep;
struct tm *localtime(const time_t *timep);

SIGSEG 将消失(尽管您的代码仍然有问题)

正确的做法是#include <time.h>

于 2013-05-25T05:53:18.013 回答
0

我在 while(1) 完成后包含了 fclose(fk) 。它不会给出任何错误并且在终端上可以正常工作。但在这种情况下,它不会写入 file.txt。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <limits.h>
#include <time.h>

#define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
#define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
#define EVENT_SIZE  ( sizeof (struct inotify_event) ) /*size of one event*/
#define BUF_LEN     ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
#define SIZE 20000
int main( int argc, char **argv ) 
{
  int length, i = 0, wd;
  int fd;
  char buffer[BUF_LEN];


    time_t timer;
    char buffer2[25];
    char *s=NULL;
    struct tm* tm_info;

  FILE *fk;
  //char buffer[EVENT_BUF_LEN];
  char str1[] = "File has been updated at time :";
    time(&timer);
    tm_info = localtime(&timer);
    strftime(buffer2, 25, "%Y:%m:%d %H:%M:%S", tm_info);
   fk = fopen( "/home/technoworld/Desktop/file.txt" , "w" );



  /* Initialize Inotify*/
  fd = inotify_init();
  if ( fd < 0 ) {
    perror( "Couldn't initialize inotify");
  }

  /* add watch to starting directory */
  wd = inotify_add_watch(fd, argv[1], IN_MODIFY ); 

  if (wd == -1)
    {
      printf("Couldn't add listen to %s\n",argv[1]);
    }
  else
    {
      printf("Listening: %s\n",argv[1]);
    }

  /* do it forever*/
  while(1)
    {
      i = 0;
      length = read( fd, buffer, BUF_LEN );  

      if ( length < 0 ) {
        perror( "read" );
      }  

      while ( i < length ) {
        struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
        if ( event->len ) {

          if ( event->mask & IN_MODIFY) {
            if (event->mask & IN_ISDIR)
              printf( "The directory %s was modified.\n", event->name );       
            else
        {
                    printf( "The file %s was modified at %s \n", event->name, buffer2 ); 
                    fprintf(fk,"%s %s %c",str1, buffer2, '\n');
        }
          }

            i += EVENT_SIZE + event->len;
        }
      }

    }
fclose(fk);
  /* Clean up*/
  inotify_rm_watch( fd, wd );
  close( fd );

  return 0;
}
于 2013-05-25T06:15:46.250 回答