gabaman

Hozzászólások

10 bejegyzés megtekintése - 361-370 / 2,173
  • Szerző
    Bejegyzés
  • Hozzászólás: Szegmens hiba – de miért? #2164585
    gabaman
    Felhasználó

      Ha már megemlítetted a sebességet, akkor legyen már gyors is:

      Code:
      const char *nc_filetype ( struct stat *st ) {
          switch ( st->st_mode & S_IFMT ) {
            case S_IFDIR:
              return „könyvtár”;
            case S_IFCHR:
              return „karakter eszköz”;
            case S_IFBLK:
              return „blokkeszköz”;
            case S_IFREG:
              return „szabályos”;
            case S_IFIFO:
              return „fifo”;
            case S_IFLNK:
              return „link”;
            case S_IFSOCK:
              return „socket”;
            default:
              break;
          }
          return „pass”;
      }
      Hozzászólás: Szegmens hiba – de miért? #2164578
      gabaman
      Felhasználó

        Ez is horror:

        Code:
        char *nc_filetype (struct stat *st) {
            char *result;

            result = malloc(30);
            sprintf(result,””);
            if (S_ISDIR(st->st_mode)) {
                sprintf(result,”könyvtár”);
            } else if (S_ISCHR(st->st_mode)) {
                sprintf(result,”karakter eszköz”);
            } else if (S_ISBLK(st->st_mode)) {
                sprintf(result,”blokkeszköz”);
            } else if (S_ISREG(st->st_mode)) {
                sprintf(result,”szabályos”);
            } else if (S_ISFIFO(st->st_mode)) {
                sprintf(result,”fifo”);
            } else if (S_ISLNK(st->st_mode)) {
                sprintf(result,”link”);
            } else if (S_ISSOCK(st->st_mode)) {
                sprintf(result,”socket”);
            } else sprintf(result,”pass”);

            return result;
        }

        Így jobb:

        Code:
        const char *nc_filetype (struct stat *st) {
            if (S_ISDIR(st->st_mode)) {
                return „könyvtár”;
            } else if (S_ISCHR(st->st_mode)) {
                return „karakter eszköz”;
            } else if (S_ISBLK(st->st_mode)) {
                return „blokkeszköz”;
            } else if (S_ISREG(st->st_mode)) {
                return „szabályos”;
            } else if (S_ISFIFO(st->st_mode)) {
                return „fifo”;
            } else if (S_ISLNK(st->st_mode)) {
                return „link”;
            } else if (S_ISSOCK(st->st_mode)) {
                return „socket”;
            }

            return „pass”;
        }

        Hozzászólás: Szegmens hiba – de miért? #2164579
        gabaman
        Felhasználó

          Ez is horror:

          Code:
          char *nc_filetype (struct stat *st) {
              char *result;

              result = malloc(30);
              sprintf(result,””);
              if (S_ISDIR(st->st_mode)) {
                  sprintf(result,”könyvtár”);
              } else if (S_ISCHR(st->st_mode)) {
                  sprintf(result,”karakter eszköz”);
              } else if (S_ISBLK(st->st_mode)) {
                  sprintf(result,”blokkeszköz”);
              } else if (S_ISREG(st->st_mode)) {
                  sprintf(result,”szabályos”);
              } else if (S_ISFIFO(st->st_mode)) {
                  sprintf(result,”fifo”);
              } else if (S_ISLNK(st->st_mode)) {
                  sprintf(result,”link”);
              } else if (S_ISSOCK(st->st_mode)) {
                  sprintf(result,”socket”);
              } else sprintf(result,”pass”);

              return result;
          }

          Így jobb:

          Code:
          const char *nc_filetype (struct stat *st) {
              if (S_ISDIR(st->st_mode)) {
                  return „könyvtár”;
              } else if (S_ISCHR(st->st_mode)) {
                  return „karakter eszköz”;
              } else if (S_ISBLK(st->st_mode)) {
                  return „blokkeszköz”;
              } else if (S_ISREG(st->st_mode)) {
                  return „szabályos”;
              } else if (S_ISFIFO(st->st_mode)) {
                  return „fifo”;
              } else if (S_ISLNK(st->st_mode)) {
                  return „link”;
              } else if (S_ISSOCK(st->st_mode)) {
                  return „socket”;
              }

              return „pass”;
          }

          Hozzászólás: Szegmens hiba – de miért? #2164574
          gabaman
          Felhasználó

            Nálam is megy, bár hogy az nc_strcat() visszatérési értéke egy dinamikus tömb, ami azonnal megkap egy eljárás paramétere… Milyen gcc-t használsz, mert a régiek eléggé bugosak.

            sprintf(result,””);
            Ez jobb nála:
            result[0] = 0;

            Hozzászólás: Szegmens hiba – de miért? #2164575
            gabaman
            Felhasználó

              Nálam is megy, bár hogy az nc_strcat() visszatérési értéke egy dinamikus tömb, ami azonnal megkap egy eljárás paramétere… Milyen gcc-t használsz, mert a régiek eléggé bugosak.

              sprintf(result,””);
              Ez jobb nála:
              result[0] = 0;

              Hozzászólás: Szegmens hiba – de miért? #2164562
              gabaman
              Felhasználó

                result = malloc((strlen(first)+strlen(last))*sizeof(char));

                Ez ugye vicc? Így sohasem szabad sztringnek helyet lefoglalni! Legalábbos C/C++ nyelven soha. Legalább egyet hozzá kell adni az EOS(sztring vége jel) miatt, jelenleg még egyet a plusz ‘/’ karakternek, tehát kettőt. De legalább egyet minenképpen és minden esetben!

                result = malloc((strlen(first)+strlen(last))*sizeof(char)+2);

                Hozzászólás: Szegmens hiba – de miért? #2164563
                gabaman
                Felhasználó

                  result = malloc((strlen(first)+strlen(last))*sizeof(char));

                  Ez ugye vicc? Így sohasem szabad sztringnek helyet lefoglalni! Legalábbos C/C++ nyelven soha. Legalább egyet hozzá kell adni az EOS(sztring vége jel) miatt, jelenleg még egyet a plusz ‘/’ karakternek, tehát kettőt. De legalább egyet minenképpen és minden esetben!

                  result = malloc((strlen(first)+strlen(last))*sizeof(char)+2);

                  Hozzászólás: Szegmens hiba – de miért? #2164560
                  gabaman
                  Felhasználó
                    uzsolt wrote:
                    Lehet, hogy sz@r a gépem, de még így is szegmens hiba…
                    Próbáld már ki, nehogy már csak nálam legyen a hiba…

                    Gyanítom, hogy vagy valamit összekevertél, vagy nincs rendes függőség a fordításnál.

                    Amit kipróbáltam, arra a valgrind azt mondja hogy „malloc/free: in use at exit: 0 bytes in 0 blocks.”, szóval 60 teszt fájlnál működik.

                    Ezeket próbáltam:

                    main.c

                    Code:
                    #include
                    #include „nc.h”

                    int main (int argc , char **argv) {
                        nc_file *files;
                        int mennyi,i;

                        mennyi = nc_readdir(argv[1],&files);

                        for (i=0; i<mennyi; i++) {
                            printf("file: %sn",files[i].name);
                        }

                        nc_free(&files, mennyi);

                        return 0;
                    }

                    nc.h

                    Code:
                    #include
                    #ifndef NC_H
                    #define NC_H

                    #define pr_debug(msg) printf(„DEBUG: %s %d. %sn”,__FILE__,__LINE__,msg);

                    /// This structure stores the file’s properties.
                    typedef struct nc_file {
                        /// the name of the file
                        char *name;
                        /// the properties (from stat.h)
                        struct stat *props;
                        /// file is selected
                        char selected;
                    } nc_file;

                    /// return value: number of files, -1 if error
                    int nc_readdir (char *dir , nc_file **files);
                    /// get string name
                    char *nc_filetype (struct stat *st);
                    /// free nc_file structure
                    void nc_free (nc_file **files, int size);

                    #endif

                    nc.c

                    Code:
                    #include
                    #include
                    #include
                    #include
                    #include
                    #include

                    #include „nc.h”

                    int nc_readdir (char *dir , nc_file **files ) {
                        DIR *d_dir;
                        struct dirent *d_file;
                        int max=0;
                        int nof=0 , idx=0;
                        char *filename;

                        d_dir = opendir(dir);
                        if (!d_dir) {
                            return -1;
                        }

                        (*files) = 0;

                        errno = 0;
                        while ( (d_file=readdir(d_dir)) != NULL ) {
                            nof++;
                            if ((max == 0) || ( max == (nof) )) {
                                max += 50;
                                (*files) = realloc(*files,sizeof(nc_file)*max);
                                if ((*files) == NULL) {
                                    return -1;
                                }
                            }
                            (*files)[idx].name = strdup((char*)(d_file->d_name));
                            (*files)[idx].props = (struct stat*) malloc(sizeof(struct stat));
                            filename = (char*) malloc ((strlen(dir)+strlen(d_file->d_name))*sizeof(char)+2);
                            strcpy(filename, dir);
                            strcat(filename, „/”);
                            strncat(filename, d_file->d_name, d_file->d_reclen);
                            stat(filename, (*files)[idx].props);
                            free(filename);
                            idx++;
                        }
                        if (errno != 0) {
                            return -1;
                        }
                        closedir(d_dir);
                        return nof;
                    }

                    void nc_free (nc_file **files, int size)
                    {
                        int i;

                        for (i=0;i<size;i++) {
                            free((*files)[i].name);
                            free((*files)[i].props);
                        }

                        free (*files);
                    }

                    Hogy ne legyen offset probléma, fordítsd egyben tárgykód nélkül nélkül:
                    $ gcc -Wall -g main.c nc.c -o nc

                    Hozzászólás: Szegmens hiba – de miért? #2164561
                    gabaman
                    Felhasználó
                      uzsolt wrote:
                      Lehet, hogy sz@r a gépem, de még így is szegmens hiba…
                      Próbáld már ki, nehogy már csak nálam legyen a hiba…

                      Gyanítom, hogy vagy valamit összekevertél, vagy nincs rendes függőség a fordításnál.

                      Amit kipróbáltam, arra a valgrind azt mondja hogy „malloc/free: in use at exit: 0 bytes in 0 blocks.”, szóval 60 teszt fájlnál működik.

                      Ezeket próbáltam:

                      main.c

                      Code:
                      #include
                      #include „nc.h”

                      int main (int argc , char **argv) {
                          nc_file *files;
                          int mennyi,i;

                          mennyi = nc_readdir(argv[1],&files);

                          for (i=0; i<mennyi; i++) {
                              printf("file: %sn",files[i].name);
                          }

                          nc_free(&files, mennyi);

                          return 0;
                      }

                      nc.h

                      Code:
                      #include
                      #ifndef NC_H
                      #define NC_H

                      #define pr_debug(msg) printf(„DEBUG: %s %d. %sn”,__FILE__,__LINE__,msg);

                      /// This structure stores the file’s properties.
                      typedef struct nc_file {
                          /// the name of the file
                          char *name;
                          /// the properties (from stat.h)
                          struct stat *props;
                          /// file is selected
                          char selected;
                      } nc_file;

                      /// return value: number of files, -1 if error
                      int nc_readdir (char *dir , nc_file **files);
                      /// get string name
                      char *nc_filetype (struct stat *st);
                      /// free nc_file structure
                      void nc_free (nc_file **files, int size);

                      #endif

                      nc.c

                      Code:
                      #include
                      #include
                      #include
                      #include
                      #include
                      #include

                      #include „nc.h”

                      int nc_readdir (char *dir , nc_file **files ) {
                          DIR *d_dir;
                          struct dirent *d_file;
                          int max=0;
                          int nof=0 , idx=0;
                          char *filename;

                          d_dir = opendir(dir);
                          if (!d_dir) {
                              return -1;
                          }

                          (*files) = 0;

                          errno = 0;
                          while ( (d_file=readdir(d_dir)) != NULL ) {
                              nof++;
                              if ((max == 0) || ( max == (nof) )) {
                                  max += 50;
                                  (*files) = realloc(*files,sizeof(nc_file)*max);
                                  if ((*files) == NULL) {
                                      return -1;
                                  }
                              }
                              (*files)[idx].name = strdup((char*)(d_file->d_name));
                              (*files)[idx].props = (struct stat*) malloc(sizeof(struct stat));
                              filename = (char*) malloc ((strlen(dir)+strlen(d_file->d_name))*sizeof(char)+2);
                              strcpy(filename, dir);
                              strcat(filename, „/”);
                              strncat(filename, d_file->d_name, d_file->d_reclen);
                              stat(filename, (*files)[idx].props);
                              free(filename);
                              idx++;
                          }
                          if (errno != 0) {
                              return -1;
                          }
                          closedir(d_dir);
                          return nof;
                      }

                      void nc_free (nc_file **files, int size)
                      {
                          int i;

                          for (i=0;i<size;i++) {
                              free((*files)[i].name);
                              free((*files)[i].props);
                          }

                          free (*files);
                      }

                      Hogy ne legyen offset probléma, fordítsd egyben tárgykód nélkül nélkül:
                      $ gcc -Wall -g main.c nc.c -o nc

                      Hozzászólás: Szegmens hiba – de miért? #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);
                      10 bejegyzés megtekintése - 361-370 / 2,173