Szegmens hiba – de miért?

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

10 bejegyzés megtekintése - 41-50 / 77
  • Szerző
    Bejegyzés
  • #2164558
    uzsolt
    Felhasználó

      Kiváncsi vagyok…

      #2164559
      uzsolt
      Felhasználó

        Kiváncsi vagyok…

        #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

          #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

            #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);

              #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);

                #2164564
                pointux
                Felhasználó

                  Különben nálam is lefut.

                  #2164565
                  pointux
                  Felhasználó

                    Különben nálam is lefut.

                    #2164566
                    uzsolt
                    Felhasználó

                      gabaman: a tied működik (nálam). Javítottam a stringes elírást is, de úgy se jó.
                      vizsla: az én verzióm megy nálad?

                      #2164567
                      uzsolt
                      Felhasználó

                        gabaman: a tied működik (nálam). Javítottam a stringes elírást is, de úgy se jó.
                        vizsla: az én verzióm megy nálad?

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