Je pense qu'il te faut utiliser l'allocation dynamique pour tes tableaux de caractères.
Là ils sont en statiques : tu demandes donc 2* 5238807 octets soit environ 10,4Mo ce qui doit exploser la pile.
Le code suivant fonctionne correctement sur mon pc, avec de très grandes valeurs : ./sandbox 523880700 523880700
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
| #include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
int main (int argc, char **argv){
if(argc != 3){
printf("%d",argc);
printf("Please, relauch program with the length of the two words\n");
return 1;
}
else{
char alphabet[] = " abcdefghijklmnopqrstuvwxyz";
unsigned int lengthFirstWord;
unsigned char integer;
unsigned int i;
srand(time(NULL));
/* For the first word*/
lengthFirstWord = atoi(argv[1]);
printf("atoi(argv[1]) = %u\n", lengthFirstWord );
char *firstWord = malloc( lengthFirstWord+1 );
/* verifier mieux le malloc*/assert(firstWord);
for (i = 0; i < lengthFirstWord; i++){
integer = rand() % (26) + 1;
*(firstWord+i) = alphabet[integer];
}
firstWord[i] = '\0';
printf("i = %u, strlen(firstword) = %u\n", i, strlen(firstWord) );
free( firstWord );
return 0;
}
} |
PS : il faut bien faire
integer = rand() % (26) + 1;
car cela implique integer inclut dans [1; 26] et acceder à alphabet[26] correspond à la 27ieme case, soit Z. Désolé.
Partager