Bonjour,
J'ouvre cette nouvelle discussion car quand j'exécute ce programme, je me retrouve avec un Segmentation fault (core dumped).
Le problème vient probablement d'une mauvaise utilisation des pointeurs mais je ne sais pas précisément où ça coince.
Je compile avec gcc -Wall -Werror -W, jusque là tout se passe bien.
En utilisant le débugger Valgrind avec l'option "-v" voici ce qu'il affiche en sortie (ici je n'appel que la première fonction, mes l'appel à la deuxième pose aussi des problèmes de lectures/écritures en mémoire):Code:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 #include <stdio.h> #include <string.h> #include <stdlib.h> char * miroir(const char *s) { int i=0; while(s[i] != '\n'){ i++; } char *z = malloc(i*sizeof(char)); char x[i]; int cpt = 0; while(i > 0){ x[cpt]=s[i-1]; i--; cpt++; } strcpy(z, x); return (char *)z; } char * saisie(){ char *z = malloc(50*sizeof(char)); int a = getchar(); int limit = 0; int memory = 2; if(a == '\n'){ printf("Chaine vaut null\n"); }else{ while(a != '\n'){ limit++; if(limit % 50 == 0){ char *tmp = realloc(z, 50*memory); if(tmp == NULL){ printf("Espace mémoire insuffisant.\n"); char fin[] = {'\0'}; z = strcat(z, fin); printf("La dernière chaine sauvegardé est %s", z); return (char *)z; } z = tmp; memory++; } char tmp[] = {a}; z = strcat(z,tmp); a = getchar(); } } printf("%s",z); return (char *)z; } int main(int argc, char **argv) { //char *mir = "Bonjour\n"; //printf("%s", miroir(mir)); char *param = "-m"; if(strcmp(argv[argc-2], param)==0 && argv[argc-1] != NULL){ char *tmp = miroir(argv[argc-1]); printf("-m : %s",tmp); }else{ printf("Erreur : %d : %s\n",strcmp(argv[argc-2], param), argv[argc-2]); } return 0; }
Merci d'avance pour votre aideCode:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 ==4903== Memcheck, a memory error detector ==4903== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==4903== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info ==4903== Command: ./a.out ==4903== --4903-- Valgrind options: --4903-- -v --4903-- Contents of /proc/version: --4903-- Linux version 4.10.0-19-generic (buildd@lcy01-13) (gcc version 6.3.0 20170321 (Ubuntu 6.3.0-10ubuntu1) ) #21-Ubuntu SMP Thu Apr 6 17:04:57 UTC 2017 --4903-- --4903-- Arch and hwcaps: AMD64, LittleEndian, amd64-cx16-rdtscp-sse3-avx --4903-- Page sizes: currently 4096, max supported 4096 --4903-- Valgrind library directory: /usr/lib/valgrind --4903-- Reading syms from /home/thibault/langageC/TP4/a.out --4903-- Reading syms from /lib/x86_64-linux-gnu/ld-2.24.so --4903-- Considering /lib/x86_64-linux-gnu/ld-2.24.so .. --4903-- .. CRC mismatch (computed 4b0c5a7e wanted 89f5325d) --4903-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.24.so .. --4903-- .. CRC is valid --4903-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux --4903-- Considering /usr/lib/valgrind/memcheck-amd64-linux .. --4903-- .. CRC mismatch (computed c2dc3c92 wanted ad3f4971) --4903-- object doesn't have a symbol table --4903-- object doesn't have a dynamic symbol table --4903-- Scheduler: using generic scheduler lock implementation. --4903-- Reading suppressions file: /usr/lib/valgrind/default.supp ==4903== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-4903-by-thibault-on-??? ==4903== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-4903-by-thibault-on-??? ==4903== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-4903-by-thibault-on-??? ==4903== ==4903== TO CONTROL THIS PROCESS USING vgdb (which you probably ==4903== don't want to do, unless you know exactly what you're doing, ==4903== or are doing some strange experiment): ==4903== /usr/lib/valgrind/../../bin/vgdb --pid=4903 ...command... ==4903== ==4903== TO DEBUG THIS PROCESS USING GDB: start GDB like this ==4903== /path/to/gdb ./a.out ==4903== and then give GDB the following command ==4903== target remote | /usr/lib/valgrind/../../bin/vgdb --pid=4903 ==4903== --pid is optional if only one valgrind process is running ==4903== --4903-- REDIR: 0x401d280 (ld-linux-x86-64.so.2:strlen) redirected to 0x380a6091 (???) --4903-- REDIR: 0x401bbd0 (ld-linux-x86-64.so.2:index) redirected to 0x380a60ab (???) --4903-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so --4903-- Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so .. --4903-- .. CRC mismatch (computed da1984fa wanted 7644421c) --4903-- object doesn't have a symbol table --4903-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so --4903-- Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so .. --4903-- .. CRC mismatch (computed bbbdc1f5 wanted 3c7fb4f9) --4903-- object doesn't have a symbol table ==4903== WARNING: new redirection conflicts with existing -- ignoring it --4903-- old: 0x0401d280 (strlen ) R-> (0000.0) 0x380a6091 ??? --4903-- new: 0x0401d280 (strlen ) R-> (2007.0) 0x04c30de0 strlen --4903-- REDIR: 0x401bdf0 (ld-linux-x86-64.so.2:strcmp) redirected to 0x4c31ee0 (strcmp) --4903-- REDIR: 0x401dd90 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x4c352b0 (mempcpy) --4903-- Reading syms from /lib/x86_64-linux-gnu/libc-2.24.so --4903-- Considering /lib/x86_64-linux-gnu/libc-2.24.so .. --4903-- .. CRC mismatch (computed 1f08dfe0 wanted 2cd0873a) --4903-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.24.so .. --4903-- .. CRC is valid --4903-- REDIR: 0x4ecb020 (libc.so.6:strcasecmp) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ec6990 (libc.so.6:strcspn) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ecd310 (libc.so.6:strncasecmp) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ec8e00 (libc.so.6:strpbrk) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ec9190 (libc.so.6:strspn) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4eca6a0 (libc.so.6:memmove) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ec6870 (libc.so.6:strcpy) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ec53e0 (libc.so.6:strcmp) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ec4f90 (libc.so.6:strcat) redirected to 0x4a28740 (_vgnU_ifunc_wrapper) --4903-- REDIR: 0x4ec8b10 (libc.so.6:rindex) redirected to 0x4c30770 (rindex) --4903-- REDIR: 0x4eda4c0 (libc.so.6:__strcmp_sse2_unaligned) redirected to 0x4c31da0 (strcmp) ==4903== Invalid read of size 1 ==4903== at 0x4EDA4C0: __strcmp_sse2_unaligned (strcmp-sse2-unaligned.S:24) ==4903== by 0x108B84: main (in /home/thibault/langageC/TP4/a.out) ==4903== Address 0x1 is not stack'd, malloc'd or (recently) free'd ==4903== ==4903== ==4903== Process terminating with default action of signal 11 (SIGSEGV) ==4903== Access not within mapped region at address 0x1 ==4903== at 0x4EDA4C0: __strcmp_sse2_unaligned (strcmp-sse2-unaligned.S:24) ==4903== by 0x108B84: main (in /home/thibault/langageC/TP4/a.out) ==4903== If you believe this happened as a result of a stack ==4903== overflow in your program's main thread (unlikely but ==4903== possible), you can try to increase the size of the ==4903== main thread stack using the --main-stacksize= flag. ==4903== The main thread stack size used in this run was 8388608. --4903-- REDIR: 0x4ec00e0 (libc.so.6:free) redirected to 0x4c2ecf0 (free) ==4903== ==4903== HEAP SUMMARY: ==4903== in use at exit: 0 bytes in 0 blocks ==4903== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==4903== ==4903== All heap blocks were freed -- no leaks are possible ==4903== ==4903== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) ==4903== ==4903== 1 errors in context 1 of 1: ==4903== Invalid read of size 1 ==4903== at 0x4EDA4C0: __strcmp_sse2_unaligned (strcmp-sse2-unaligned.S:24) ==4903== by 0x108B84: main (in /home/thibault/langageC/TP4/a.out) ==4903== Address 0x1 is not stack'd, malloc'd or (recently) free'd ==4903== ==4903== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) Segmentation fault (core dumped)