Mijn programma werkt naar behoren alleen geeft een segfault aan het einde..
ook heb ik het geprobeerd met in de functie een malloc voor de char** en met een free aan het einde v/h programma..
Hierbij de source en daaronder de gdb output.
Kan iemand mij vertellen hoe ik deze segfault netjes oplos ?
ook heb ik het geprobeerd met in de functie een malloc voor de char** en met een free aan het einde v/h programma..
Hierbij de source en daaronder de gdb output.
Kan iemand mij vertellen hoe ik deze segfault netjes oplos ?
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| #include <dirent.h> #include <string.h> main() { int i,itar; char **tarlist; itar = dirlist("/etc/",".conf",tarlist); printf("\nLets Go:\n"); for(i=0;i<itar;i++) { printf("%s\n",tarlist[i]); free(tarlist[i]); } printf("\nDone\n"); //free(tarlist); } int dirlist(const char* dirname, const char* needle, char **tarlist) { struct dirent **namelist; int n,i,itar = 0; n = scandir(dirname, &namelist, 0, alphasort); //tarlist = (char**)malloc(sizeof(char *)*n); if (n < 0) perror("scandir"); else { i=0; for(i=0;i<n;i++) { printf("%s\n", namelist[i]->d_name); if(strstr(namelist[i]->d_name,needle)!=NULL) { printf(" HIT %s contains %s (hit nr %d)\n",namelist[i]->d_name,needle,itar); tarlist[itar] = (char*)malloc(256); strncpy(tarlist[itar],namelist[i]->d_name,255); itar++; } free(namelist[i]); } free(namelist); } return(itar); } |
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $ gdb test ./core GNU gdb 2002-04-01-cvs Copyright 2002 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-linux"...(no debugging symbols found)... Core was generated by `./test'. Program terminated with signal 11, Segmentation fault. Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done. Loaded symbols for /lib/libc.so.6 Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done. Loaded symbols for /lib/ld-linux.so.2 #0 0x40084a90 in free () from /lib/libc.so.6 (gdb) |
[ Voor 11% gewijzigd door xychix op 14-10-2003 15:10 ]
Every failure offers you a new opportunity! | Lokatie database|GoT - Notepad