Erreur de segmentation dans strtol
Bonjour,
J'ai une erreur de segmentation que je n'arrive pas à comprendre. Mon code est le suivant
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
char **endp;
n = (int) strtol(argv[1], endp, 10);
printf("%d \t %d \t %d \n", n, 4, 4);
return 0;
} |
Quand je compile ce code avec gcc puis que je l'exécute par exemple avec ./main 4 si main est le nom de mon programme compilé, j'obtiens une erreur de segmentation. Voici le rapport de valgrind :
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
| ==9888== Memcheck, a memory error detector
==9888== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==9888== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==9888== Command: ./main 4
==9888==
==9888== Conditional jump or move depends on uninitialised value(s)
==9888== at 0x4077A42: ____strtol_l_internal (strtol_l.c:489)
==9888== by 0x407761F: strtol (strtol.c:110)
==9888== by 0x804841C: main (in /home/tlaffarg/Documents/Simulations/A/main)
==9888==
==9888== Use of uninitialised value of size 4
==9888== at 0x4077A47: ____strtol_l_internal (strtol_l.c:490)
==9888== by 0x407761F: strtol (strtol.c:110)
==9888== by 0x804841C: main (in /home/tlaffarg/Documents/Simulations/A/main)
==9888==
==9888==
==9888== Process terminating with default action of signal 11 (SIGSEGV)
==9888== Bad permissions for mapped region at address 0x4188FF4
==9888== at 0x4077A47: ____strtol_l_internal (strtol_l.c:490)
==9888== by 0x407761F: strtol (strtol.c:110)
==9888== by 0x804841C: main (in /home/tlaffarg/Documents/Simulations/A/main)
==9888==
==9888== HEAP SUMMARY:
==9888== in use at exit: 0 bytes in 0 blocks
==9888== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==9888==
==9888== All heap blocks were freed -- no leaks are possible
==9888==
==9888== For counts of detected and suppressed errors, rerun with: -v
==9888== Use --track-origins=yes to see where uninitialised values come from
==9888== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 7) |
Ce qui m'étonne particulièrement est que les trois codes sources suivant, qui viennent du précédent légèrement modifié, fonctionnent :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
char **endp; // Buffer used to read the input variables
n = (int) strtol(argv[1], endp, 10);
printf("%d \t %d \n", n, 4);
return 0;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
char **endp; // Buffer used to read the input variables
n = 4;
printf("%d \t %d \t %d \n", n, 4, 4);
return 0;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
char *endp;
n = (int) strtol(argv[1], &endp, 10);
printf("%d \t %d \t %d \n", n, 4, 4);
return 0;
} |
Quelqu'un pourrait-il m'expliquer ce qui ne va pas dans le premier code?
Merci d'avance!