Szegmens hiba – de miért?

Kezdőlap Fórumok Programozás Szegmens hiba – de miért?

10 bejegyzés megtekintése - 21-30 / 77
  • Szerző
    Bejegyzés
  • #2164538
    uzsolt
    Felhasználó
      gabaman wrote:
      Mi a nof? A fájlok száma?
      Annak készült (number of files).

      gabaman wrote:
      Akkor méret típusú.
      Mármint size_t? Úgyse jó 🙁
      Egy plusz index bevezetésével se (ami int típusú, nulláról indul, és a ciklus végén növelem).
      #2164539
      uzsolt
      Felhasználó
        gabaman wrote:
        Mi a nof? A fájlok száma?
        Annak készült (number of files).

        gabaman wrote:
        Akkor méret típusú.
        Mármint size_t? Úgyse jó 🙁
        Egy plusz index bevezetésével se (ami int típusú, nulláról indul, és a ciklus végén növelem).
        #2164540
        gabaman
        Felhasználó

          Dehogynem jó amit írtam. Ugyanis a cikluson belül index, kívül meg méret típusú a nof, de csak a kódodban (ha a ciklus végén növeled a nof változót). Gondold csak végig, miben különbözik a következő kódrészlet a tiedtől:

          Code:
          int nof = 0, idx = 0;

          (…)

              while ( (d_file=readdir(d_dir)) != NULL ) {
                  nof++;
          (…)
                  if ( max == (nof) ) {
          (…)
                  (*files)[idx].name = strdup(d_file->d_name);
                  idx++;
                } // end of ‘while’
          (…)

              return nof;
          }

          #2164541
          gabaman
          Felhasználó

            Dehogynem jó amit írtam. Ugyanis a cikluson belül index, kívül meg méret típusú a nof, de csak a kódodban (ha a ciklus végén növeled a nof változót). Gondold csak végig, miben különbözik a következő kódrészlet a tiedtől:

            Code:
            int nof = 0, idx = 0;

            (…)

                while ( (d_file=readdir(d_dir)) != NULL ) {
                    nof++;
            (…)
                    if ( max == (nof) ) {
            (…)
                    (*files)[idx].name = strdup(d_file->d_name);
                    idx++;
                  } // end of ‘while’
            (…)

                return nof;
            }

            #2164542
            uzsolt
            Felhasználó

              Értem, mire gondolsz. Csak a probléma, hogy így is szegmens hibát produkál 🙁
              30, 31-ig eljut, aztán kalap…

              Code:
              int nc_readdir (char *dir , nc_file **files ) {
                  DIR *d_dir;
                  struct dirent *d_file;
                  struct stat prop;
                  int max = 30;
                  /// return value – Number Of Files
                  int nof=0 , idx=0;

                  d_dir = opendir(dir);
                  if (!d_dir) {
              #ifdef DEBUG
                      pr_debug(„couldn’t read”);
              #endif
                      return -1;
                  }

              #ifdef DEBUG
                  pr_debug(„malloc files”);
              #endif
                  (*files) = malloc(sizeof(nc_file)*(max));
                  if ( (*files) == NULL ) return -1;
              #ifdef DEBUG
                  pr_debug(„mallocing ok”);
              #endif

                  while ( (d_file=readdir(d_dir)) != NULL ) {
                      nof++;
                      if ( max == (nof) ) {
              #ifdef DEBUG
                          pr_debug(„reallocing”);
              #endif
                          max += 50;
              #ifdef DEBUG
                          printf(„max = %dn”,max);
              #endif
                          (*files) = realloc(*files,sizeof(nc_file)*max);
                          if ((*files) == NULL) {
                              return -1;
                          }
              #ifdef DEBUG
                          pr_debug(„realloc ok”);
              #endif
                      }
              #ifdef DEBUG
                      pr_debug(„strdup”);
                      printf(„%d. %sn”,nof,d_file->d_name);
              #endif
                      (*files)[idx].name = strdup((char*)(d_file->d_name));
              #ifdef DEBUG
                      pr_debug(„strdup ok”);
              #endif
              #ifdef DEBUG
                      pr_debug(„stat”);
              #endif
                      (*files)[idx].props=malloc(sizeof(stat));
              // INNEN KIVETTEM AZ nc_strcat-os SORT!!!
              #ifdef DEBUG
                      pr_debug(„stat ok”);
              #endif
                      idx++;
                  }
                  closedir(d_dir);
                  return nof;
              }

              Javaslom, hogy próbáld ki, biztos valami hülyeség, amit így „ránézésre” nem veszel észre. Én meg működés közben sem…

              #2164543
              uzsolt
              Felhasználó

                Értem, mire gondolsz. Csak a probléma, hogy így is szegmens hibát produkál 🙁
                30, 31-ig eljut, aztán kalap…

                Code:
                int nc_readdir (char *dir , nc_file **files ) {
                    DIR *d_dir;
                    struct dirent *d_file;
                    struct stat prop;
                    int max = 30;
                    /// return value – Number Of Files
                    int nof=0 , idx=0;

                    d_dir = opendir(dir);
                    if (!d_dir) {
                #ifdef DEBUG
                        pr_debug(„couldn’t read”);
                #endif
                        return -1;
                    }

                #ifdef DEBUG
                    pr_debug(„malloc files”);
                #endif
                    (*files) = malloc(sizeof(nc_file)*(max));
                    if ( (*files) == NULL ) return -1;
                #ifdef DEBUG
                    pr_debug(„mallocing ok”);
                #endif

                    while ( (d_file=readdir(d_dir)) != NULL ) {
                        nof++;
                        if ( max == (nof) ) {
                #ifdef DEBUG
                            pr_debug(„reallocing”);
                #endif
                            max += 50;
                #ifdef DEBUG
                            printf(„max = %dn”,max);
                #endif
                            (*files) = realloc(*files,sizeof(nc_file)*max);
                            if ((*files) == NULL) {
                                return -1;
                            }
                #ifdef DEBUG
                            pr_debug(„realloc ok”);
                #endif
                        }
                #ifdef DEBUG
                        pr_debug(„strdup”);
                        printf(„%d. %sn”,nof,d_file->d_name);
                #endif
                        (*files)[idx].name = strdup((char*)(d_file->d_name));
                #ifdef DEBUG
                        pr_debug(„strdup ok”);
                #endif
                #ifdef DEBUG
                        pr_debug(„stat”);
                #endif
                        (*files)[idx].props=malloc(sizeof(stat));
                // INNEN KIVETTEM AZ nc_strcat-os SORT!!!
                #ifdef DEBUG
                        pr_debug(„stat ok”);
                #endif
                        idx++;
                    }
                    closedir(d_dir);
                    return nof;
                }

                Javaslom, hogy próbáld ki, biztos valami hülyeség, amit így „ránézésre” nem veszel észre. Én meg működés közben sem…

                #2164544
                gabaman
                Felhasználó

                  A hiba oka:

                  Code:
                  int stat(const char *path, struct stat *buf);

                  vs

                  Code:
                            struct stat {
                                dev_t    st_dev;    /* ID of device containing file */
                                ino_t    st_ino;    /* inode number */
                                mode_t    st_mode;    /* protection */
                                nlink_t  st_nlink;  /* number of hard links */
                                uid_t    st_uid;    /* user ID of owner */
                                gid_t    st_gid;    /* group ID of owner */
                                dev_t    st_rdev;    /* device ID (if special file) */
                                off_t    st_size;    /* total size, in bytes */
                                blksize_t st_blksize; /* blocksize for filesystem I/O */
                                blkcnt_t  st_blocks;  /* number of blocks allocated */
                                time_t    st_atime;  /* time of last access */
                                time_t    st_mtime;  /* time of last modification */
                                time_t    st_ctime;  /* time of last status change */
                            };

                  Azaz:

                  Code:
                  (*files)[idx].props=malloc(sizeof(stat));

                  vs

                  Code:
                  (*files)[idx].props=malloc(sizeof(struct stat));

                  😮  😮  😮

                  Mellesleg az nc_strcat() ez akar lenni:

                  Code:
                          strcpy(filename, dir);
                          strcat(filename, „/”);
                          strncat(filename, d_file->d_name, d_file->d_reclen);
                          stat(filename, (*files)[idx].props);
                          free(filename);
                  #2164545
                  gabaman
                  Felhasználó

                    A hiba oka:

                    Code:
                    int stat(const char *path, struct stat *buf);

                    vs

                    Code:
                              struct stat {
                                  dev_t    st_dev;    /* ID of device containing file */
                                  ino_t    st_ino;    /* inode number */
                                  mode_t    st_mode;    /* protection */
                                  nlink_t  st_nlink;  /* number of hard links */
                                  uid_t    st_uid;    /* user ID of owner */
                                  gid_t    st_gid;    /* group ID of owner */
                                  dev_t    st_rdev;    /* device ID (if special file) */
                                  off_t    st_size;    /* total size, in bytes */
                                  blksize_t st_blksize; /* blocksize for filesystem I/O */
                                  blkcnt_t  st_blocks;  /* number of blocks allocated */
                                  time_t    st_atime;  /* time of last access */
                                  time_t    st_mtime;  /* time of last modification */
                                  time_t    st_ctime;  /* time of last status change */
                              };

                    Azaz:

                    Code:
                    (*files)[idx].props=malloc(sizeof(stat));

                    vs

                    Code:
                    (*files)[idx].props=malloc(sizeof(struct stat));

                    😮  😮  😮

                    Mellesleg az nc_strcat() ez akar lenni:

                    Code:
                            strcpy(filename, dir);
                            strcat(filename, „/”);
                            strncat(filename, d_file->d_name, d_file->d_reclen);
                            stat(filename, (*files)[idx].props);
                            free(filename);
                    #2164546
                    uzsolt
                    Felhasználó

                      Így se jó (igen, az nc_strcat valami olyasmi):

                      $ ./ncc /

                      20. include
                      DEBUG: nc.c 96. strdup ok
                      DEBUG: nc.c 99. stat
                      DEBUG: nc.c 104. stat ok
                      . (könyvtár)
                      .. (könyvtár)
                      bin (könyvtár)
                      dev (könyvtár)
                      etc (könyvtár)
                      lib (könyvtár)
                      mnt (könyvtár)
                      pub (könyvtár)
                      srv (könyvtár)
                      tmp (könyvtár)
                      sys (könyvtár)
                      var (könyvtár)
                      usr (könyvtár)
                      boot (könyvtár)
                      home (könyvtár)
                      proc (könyvtár)
                      sbin (könyvtár)
                      root (könyvtár)
                      Storage (könyvtár)
                      include (könyvtár)

                      $ ./ncc /lib

                      461. libgphoto2_port
                      DEBUG: nc.c 96. strdup ok
                      DEBUG: nc.c 99. stat
                      DEBUG: nc.c 104. stat ok
                      DEBUG: nc.c 91. strdup
                      [red]462.[/red] libXevie.la
                      *** glibc detected *** malloc(): memory corruption: 0x080f5ec8 ***
                      Félbeszakítva (core dumped)

                      $ ./ncc /bin

                      382. kbdrate
                      DEBUG: nc.c 96. strdup ok
                      DEBUG: nc.c 99. stat
                      DEBUG: nc.c 104. stat ok
                      DEBUG: nc.c 91. strdup
                      [red]383.[/red] ior-decode
                      *** glibc detected *** malloc(): memory corruption: 0x080947c8 ***
                      Félbeszakítva (core dumped)

                      Egyszerűen nem értem. Szinte minden könyvtárban máskor dobja a hibát (valahol nem is, ahol „kevés” fájl van).

                      #2164547
                      uzsolt
                      Felhasználó

                        Így se jó (igen, az nc_strcat valami olyasmi):

                        $ ./ncc /

                        20. include
                        DEBUG: nc.c 96. strdup ok
                        DEBUG: nc.c 99. stat
                        DEBUG: nc.c 104. stat ok
                        . (könyvtár)
                        .. (könyvtár)
                        bin (könyvtár)
                        dev (könyvtár)
                        etc (könyvtár)
                        lib (könyvtár)
                        mnt (könyvtár)
                        pub (könyvtár)
                        srv (könyvtár)
                        tmp (könyvtár)
                        sys (könyvtár)
                        var (könyvtár)
                        usr (könyvtár)
                        boot (könyvtár)
                        home (könyvtár)
                        proc (könyvtár)
                        sbin (könyvtár)
                        root (könyvtár)
                        Storage (könyvtár)
                        include (könyvtár)

                        $ ./ncc /lib

                        461. libgphoto2_port
                        DEBUG: nc.c 96. strdup ok
                        DEBUG: nc.c 99. stat
                        DEBUG: nc.c 104. stat ok
                        DEBUG: nc.c 91. strdup
                        [red]462.[/red] libXevie.la
                        *** glibc detected *** malloc(): memory corruption: 0x080f5ec8 ***
                        Félbeszakítva (core dumped)

                        $ ./ncc /bin

                        382. kbdrate
                        DEBUG: nc.c 96. strdup ok
                        DEBUG: nc.c 99. stat
                        DEBUG: nc.c 104. stat ok
                        DEBUG: nc.c 91. strdup
                        [red]383.[/red] ior-decode
                        *** glibc detected *** malloc(): memory corruption: 0x080947c8 ***
                        Félbeszakítva (core dumped)

                        Egyszerűen nem értem. Szinte minden könyvtárban máskor dobja a hibát (valahol nem is, ahol „kevés” fájl van).

                      10 bejegyzés megtekintése - 21-30 / 77
                      • Be kell jelentkezni a hozzászóláshoz.