[Debutant][C] Problème de taille de tableau
Bonjour,
J'essaye de faire un programme me permettant de générer une quantité variable de code de 9 caractères.
Mon problème vient du fait que je peux sans problème générer 800000 codes mais pas 1000000. (J'ai une erreur Segmentation Fault). Je suppose que le problème vient d'une allocation mémoire mais je vois pas vraiment comment corriger ca.
PS : j'ai aussi un warning pendant la compilation de ce code qui est :
genCode.c: In function ‘genCode’:
genCode.c:53: attention : control reaches end of non-void function.
Pourtant la fonction est bien de type void ?
Par ailleurs, toutes suggestions pour faire mieux est la bienvenue :)
Voici le code complet :
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#define NUM_THREADS 8
// Initialisation des constantes
const char cPattern[63] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Initialisation de la structure des arguments de threads
struct threadsArgs {
char *cOp;
unsigned int iQte;
};
struct threadsArgs genCode_args_array[NUM_THREADS];
void *genCode(void *threadarg)
{
// Declaration de la structure
struct threadsArgs *args;
args = (struct threadsArgs *) threadarg;
// Initialisation des variables
int i = 0;
int iRang = 2;
int iIndex = 0;
char tmpCode[10] = "";
char cCode[args->iQte][10];
char empty[1] = "";
// On boucle sur la quantite de code desire
for(i=0; i<args->iQte; i++)
{
// On initialise la position dans la chaine
iRang = 2;
// On concatene le code produit en debut de chaine
strcat(tmpCode, args->cOp);
// On boucle du rang 2 jusqu'au rang 8
while( iRang < 9 )
{
// On genere un nombre aleatoire
iIndex = rand() % 62;
// On recupere le caractere a la position iIndex dans la chaine cPattern
tmpCode[iRang] = cPattern [iIndex];
// On incremente le rang
iRang++;
}
// On copie tmpCode dans le tableau cCode
strcpy(cCode[i], tmpCode);
// On reinitialise tmpCode
strncpy(tmpCode, empty, 1);
}
// On affiche tout les codes generes
for(i=0; i<args->iQte; i++)
{
printf("Code %d : %s\n", i, cCode[i]);
}
}
int main(void)
{
pthread_t th1;
void *ret;
// Initialisation du random
srand( time(NULL) );
genCode_args_array[1].cOp = "0B";
genCode_args_array[1].iQte = 1000000;
if (pthread_create (&th1, NULL, genCode, &genCode_args_array[1]) < 0)
{
fprintf(stderr, "Impossible de creer le thread\n");
exit(1);
}
(void)pthread_join (th1, &ret);
exit(0);
} |
[Debutant][C] Problème de taille de tableau
Bonjour :)
Merci pour ta réponse.
J'ai donc effectivement rajouter des mallocs et ca fonctionne très bien. Merci beaucoup.
Par contre je suis désolé mais je bloque complétement sur le void *.
(Edit : J'ai compris lol)
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 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 107 108 109 110 111 112 113 114 115 116 117
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#define NUM_THREADS 8
// Initialisation des constantes
const char cPattern[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Initialisation de la structure des arguments de threads
struct threadsArgs {
const char *cOp;
unsigned int iQte;
};
struct threadsArgs genCode_args_array[NUM_THREADS];
void *genCode(void *threadarg)
{
// Declaration de la structure
struct threadsArgs *args;
args = (struct threadsArgs *) threadarg;
// Initialisation des variables
int i = 0;
int iRang = 2;
int iIndex = 0;
char tmpCode[10] = "";
void *ret;
// Initialisation du tableau de la liste des codes
char **codeList;
// Allocation memoire du tableau
codeList = malloc( args->iQte * sizeof(char*));
if ( codeList == NULL )
{
fprintf(stderr, "Allocation impossible");
exit(1);
}
for( i=0; i<args->iQte; i++ )
{
codeList[i] = calloc(10, sizeof(char));
if ( codeList[i] == NULL )
{
fprintf(stderr, "Allocation impossible");
exit(1);
}
}
// On boucle sur la quantite de code desire
for(i=0; i<args->iQte; i++)
{
// On initialise la position dans la chaine
iRang = 2;
// On concatene le code produit en debut de chaine
strcpy(tmpCode, args->cOp);
// On boucle du rang 2 jusqu'au rang 8
while( iRang < 9 )
{
// On genere un nombre aleatoire
iIndex = rand() % (sizeof(cPattern) - 1);
// On recupere le caractere a la position iIndex dans la chaine cPattern
tmpCode[iRang] = cPattern [iIndex];
// On incremente le rang
iRang++;
}
// On copie tmpCode dans le tableau cCode
strcpy(codeList[i], tmpCode);
}
// On affiche tout les codes generes
for(i=0; i<args->iQte; i++)
{
printf("Code %d : %s\n", i, codeList[i]);
}
// Liberation de l'espace mémoire
for( i=0; i<args->iQte; i++ )
{
free(codeList[i]);
codeList[i] = NULL;
}
free(codeList);
codeList = NULL;
return ret;
}
int main(void)
{
pthread_t th1;
void *ret;
// Initialisation du random
srand( time(NULL) );
genCode_args_array[1].cOp = "0B";
genCode_args_array[1].iQte = 10000;
if (pthread_create (&th1, NULL, genCode, &genCode_args_array[1]) < 0)
{
fprintf(stderr, "Impossible de creer le thread.\n");
exit(1);
}
(void)pthread_join (th1, &ret);
exit(0);
} |