IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Linux Discussion :

[Debutant][C] Problème de taille de tableau


Sujet :

Linux

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 3
    Points : 4
    Points
    4
    Par défaut [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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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);
    }

  2. #2
    Membre éclairé
    Avatar de D[r]eadLock
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    504
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 504
    Points : 750
    Points
    750
    Par défaut
    - genCode renvoie un void * et non un void.
    - tu n'est pas obligé de fixer la taille de cPattern (ton 62 est sizeof(cPattern) -1 (pour le \0 en "trop"))
    - perso, je n'aime pas trop "char cCode[args->iQte][10];" et mettrais plutôt un malloc (si jamais c'est trop grand). Et à mon avis c'est ce qui se passe, i.e. le programme alloue trop sur la pile et déborde...
    - pourquoi utiliser strcat ? et pas un simple strcpy (d'ou plus besoin de "réinitialiser" (que j'aurais fait tmpCode[0] = '\0')
    - ce serait plutôt const char *cOp

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2008
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2008
    Messages : 3
    Points : 4
    Points
    4
    Par défaut [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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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);
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de taille de tableau sous IE 7
    Par chatlumo dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 01/08/2008, 15h22
  2. Problème avec taille de tableau et checkbox
    Par narcis60floyjo dans le forum Langage
    Réponses: 5
    Dernier message: 31/03/2008, 16h13
  3. problème de taille de tableau
    Par tromaltsec dans le forum Collection et Stream
    Réponses: 4
    Dernier message: 13/12/2007, 09h29
  4. Problème de taille de tableau
    Par Beush dans le forum C
    Réponses: 4
    Dernier message: 01/11/2005, 17h41
  5. Problème de taille de tableau
    Par k-nine dans le forum C
    Réponses: 6
    Dernier message: 25/09/2005, 09h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo