Kezdőlap › Fórumok › Programozás › Szegmens hiba – de miért?
- This topic has 76 hozzászólás, 6 résztvevő, and was last updated 17 years, 1 months telt el by
pointux.
-
SzerzőBejegyzés
-
2008-06-17-17:57 #2164558
Kiváncsi vagyok…
2008-06-17-17:57 #2164559Kiváncsi vagyok…
2008-06-17-17:59 #2164560uzsolt 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 nc2008-06-17-17:59 #2164561uzsolt 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 nc2008-06-17-18:07 #2164562result = 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);
2008-06-17-18:07 #2164563result = 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);
2008-06-17-18:09 #2164564Különben nálam is lefut.
2008-06-17-18:09 #2164565Különben nálam is lefut.
2008-06-17-18:12 #2164566gabaman: 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?2008-06-17-18:12 #2164567gabaman: 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? -
SzerzőBejegyzés
- Be kell jelentkezni a hozzászóláshoz.
legutóbbi hsz