Se connecter avec
S'enregistrer | Connectez-vous

Langage C erreur de segmentation

Dernière réponse : dans Programmation

Bonjour
j'aurais besoin d'un petit coup de main pour trouver une petite erreur de segmentation qui me rend dingue depuis 2 jour la.
j'ai tout essayer et je l'ai pas encore trouver voila mon code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. void frand(int n, char *pkey);
  8. char fencode(char car, char KEY);
  9.  
  10. int main( int argc , char *argv[] )
  11. {
  12. /* Déclaration des variables */
  13. int n, i;
  14. char *pkey, *pCypher1, *test, *pCypher2;
  15. FILE* fichier;
  16.  
  17.  
  18.  
  19.  
  20. /*creation de fichier*/
  21. fichier=fopen("secret.key","wb");
  22.  
  23. /*verification de l'argument d'entrée*/
  24.  
  25. if(argc==1){
  26.  
  27. printf("pas de phrase de passe\n");
  28. exit(0);
  29.  
  30. }
  31. else{
  32. /*Mise dans une variable le nombre de caractere de la phrase passe */
  33. n=strlen(argv[1]);
  34. printf("longueur=%x\n",n);
  35. /*Allocation de memoire de pkey*/
  36. if((pkey=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  37.  
  38. printf("erreur d'allocation de memoire\n");
  39. exit(0);
  40. }
  41. else{
  42.  
  43. /*generation de pkey*/
  44.  
  45. frand(n, pkey);
  46.  
  47. printf("pkey: %s\n",pkey);
  48.  
  49. /*allocation memoire de pCypher1*/
  50.  
  51. if((pCypher1=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  52. printf("erreur d'allocation memoire\n");
  53. exit(0);
  54. }
  55. else{
  56. /* XOR entre pkey et le passe */
  57. for(i=0;i<n;i++)
  58. {
  59. pCypher1[i] = fencode(pkey[i], argv[1][i] );
  60. printf("pCypher1[%d] = %c\n", i, pCypher1[i]);
  61. }
  62.  
  63. printf("pCypher1: %s\n",pCypher1);
  64.  
  65. /* Ecriture de pCypher dans le fichier secret.key */
  66. fwrite(pCypher1, sizeof(char), n, fichier);
  67.  
  68.  
  69.  
  70.  
  71. }
  72. }
  73.  
  74.  
  75. /* Vérification */
  76. if((pCypher2=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  77. printf("erreur d'allocation memoire\n");
  78. exit(0);
  79. }
  80. else{
  81. fread(pCypher2, sizeof(char), n, fichier);
  82. fclose(fichier);
  83. printf("pCypher2: %s\n",pCypher2);
  84. }
  85. /* Vérification du decodage */
  86. if((test=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  87. printf("erreur d'allocation memoire\n");
  88. exit(0);
  89. }
  90. else{
  91. for(i=0;i<n;i++)
  92. {
  93. test[i] = fencode(pCypher2[i], argv[1][i] );
  94. printf("test[%d] = %c\n", i, test[i]);
  95. }
  96.  
  97. printf("test: %s\n",test);
  98.  
  99. /* Comparaison entre pkey et le pkey du fichier secret.key */
  100. if( strcmp(pkey, test) == NULL)
  101. {
  102. printf("vérifié avec succés");
  103. }
  104. else
  105. {
  106. printf("Echec verification");
  107. }
  108.  
  109.  
  110. }
  111. }
  112. free(pkey);
  113. free(test);
  114. free(pCypher1);
  115. free(pCypher2);
  116. fclose(fichier);
  117.  
  118. return EXIT_SUCCESS;
  119.  
  120. }
  121. /* generer pkey avec la suite de Fibanacci*/
  122. void frand(int n, char *pKey)
  123. {
  124. /* Definition des variables */
  125. int i, x,y,z;
  126. char *pkey;
  127. y = (rand()%128)+33;
  128. z = (rand()%128)+33;
  129.  
  130. for(i=0;i<n;i++)
  131. {
  132. x=y+z;
  133. if(x>128)
  134. {
  135. x = x - 128;
  136. }
  137. pkey[i] = x;
  138. printf("pkey[%d] = %c\n", i, pkey[i]);
  139. z = y;
  140. y = x;
  141. }
  142. }
  143.  
  144.  
  145. /* Fonction XOR entre deux caracteres */
  146. char fencode(char car, char KEY)
  147. {
  148. return(car^KEY);
  149. }

voila mon code
j'espère que quelqu'un pourrais trouver cette erreur :(  :??: 
merci d'avance :) 

Autres pages sur : langage erreur segmentation

Lassé par la pub ? Créez un compte

y a déjà plusieurs choses :
printf("pkey: %s\n",pkey);
printf("pCypher1: %s\n",pCypher1);
pKey et pCypher1 ne sont pas zéro terminés

strcmp(pkey, test)
pareil

tu peux appeler free sur des pointeurs non initialisés en cas d'erreur dans ton main

...


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. void frand(int n, char *pkey);
  8. char fencode(char car, char KEY);
  9.  
  10. int main( int argc , char *argv[] )
  11. {
  12. /* Déclaration des variables */
  13. int n, i;
  14. char *pkey, *pCypher1, *test, *pCypher2;
  15. FILE* fichier;
  16.  
  17.  
  18.  
  19.  
  20. /*creation de fichier*/
  21. fichier=fopen("secret.key","wb");
  22.  
  23. /*verification de l'argument d'entrée*/
  24.  
  25. if(argc==1){
  26.  
  27. printf("pas de phrase de passe\n");
  28. exit(0);
  29.  
  30. }
  31. else{
  32. /*Mise dans une variable le nombre de caractere de la phrase passe */
  33. n=strlen(argv[1]);
  34. printf("longueur=%x\n",n);
  35. /*Allocation de memoire de pkey*/
  36. if((pkey=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  37.  
  38. printf("erreur d'allocation de memoire\n");
  39. exit(0);
  40. }
  41. else{
  42.  
  43. /*generation de pkey*/
  44.  
  45. frand(n, pkey);
  46.  
  47. printf("pkey: %s\n",pkey);
  48.  
  49. /*allocation memoire de pCypher1*/
  50.  
  51. if((pCypher1=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  52. printf("erreur d'allocation memoire\n");
  53. exit(0);
  54. }
  55. else{
  56. /* XOR entre pkey et le passe */
  57. for(i=0;i<n;i++)
  58. {
  59. pCypher1[i] = fencode(pkey[i], argv[1][i] );
  60. printf("pCypher1[%d] = %c\n", i, pCypher1[i]);
  61. }
  62.  
  63. printf("pCypher1: %s\n",pCypher1);
  64.  
  65. /* Ecriture de pCypher dans le fichier secret.key */
  66. fwrite(pCypher1, sizeof(char), n, fichier);
  67.  
  68.  
  69.  
  70.  
  71. }
  72. }
  73.  
  74.  
  75. /* Vérification */
  76. if((pCypher2=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  77. printf("erreur d'allocation memoire\n");
  78. exit(0);
  79. }
  80. else{
  81. fread(pCypher2, sizeof(char), n, fichier);
  82. fclose(fichier);
  83. printf("pCypher2: %s\n",pCypher2);
  84. }
  85. /* Vérification du decodage */
  86. if((test=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
  87. printf("erreur d'allocation memoire\n");
  88. exit(0);
  89. }
  90. else{
  91. for(i=0;i<n;i++)
  92. {
  93. test[i] = fencode(pCypher2[i], argv[1][i] );
  94. printf("test[%d] = %c\n", i, test[i]);
  95. }
  96.  
  97. printf("test: %s\n",test);
  98.  
  99. /* Comparaison entre pkey et le pkey du fichier secret.key */
  100. if( strcmp(pkey, test) == NULL)
  101. {
  102. printf("vérifié avec succés");
  103. }
  104. else
  105. {
  106. printf("Echec verification");
  107. }
  108.  
  109.  
  110. }
  111. }
  112. free(pkey);
  113. free(test);
  114. free(pCypher1);
  115. free(pCypher2);
  116. fclose(fichier);
  117. system("PAUSE");
  118. getchar();
  119. return (0);
  120.  
  121. }
  122. /* generer pkey avec la suite de Fibanacci*/
  123. void frand(int n, char *pKey)
  124. {
  125. /* Definition des variables */
  126. int i, x,y,z;
  127. char *pkey;
  128. y = (rand()%128)+33;
  129. z = (rand()%128)+33;
  130.  
  131. for(i=0;i<n;i++)
  132. {
  133. x=y+z;
  134. if(x>128)
  135. {
  136. x = x - 128;
  137. }
  138. pkey[i] = x;
  139. printf("pkey[%d] = %c\n", i, pkey[i]);
  140. z = y;
  141. y = x;
  142. }
  143. }
  144.  
  145.  
  146. /* Fonction XOR entre deux caracteres */
  147. char fencode(char car, char KEY)
  148. {
  149. return(car^KEY);
  150. }


voila
j'ai toujours le meme problème
des erreur de segmentation dans le frand mais je trouve pas la ligne de l'erreur

c'est bon j'ai allouer de la mémoire pour pKey
mais maintenant ça me dit :
Genekey.c:147: error: conflicting types for ‘fcodage’
Genekey.c:59: note: previous implicit declaration of ‘fcodage’ was here

je vois pas vraiment c'est quoi comme erreur :??: 
Expert Programmation

Il se peut que l'écriture de ta fonction diffère de celle de ton prototype (surtout sur le type retourné, et aussi sur les paramètres), ou alors tu as utilisé le même nom de fonction/variable 2 fois.
Lassé par la pub ? Créez un compte
Tom's guide dans le monde