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

Bibliothèque standard C Discussion :

Problème avec realloc et struct


Sujet :

Bibliothèque standard C

  1. #1
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut Problème avec realloc et struct
    Bonjour, j'essaie de ré allouer de la place pour ma structure :

    Voilà en gros le code (j'ai mis que ce qu'il me faut):

    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
     
    struct life
    {
        struct pos position;
        struct col color;
        int time_life;
        int activate; /* 0:non, 1:oui */
        SDL_Surface *surface;
        int lifes;
     
    };
     
    int main(void)
    {
    struct life *myStruct = NULL;
     
    /* allocation et plein de trucs qui sont corrects */
     
    nbr = myStruct->lifes+1;
    myStruct->lifes+1;
    myStruct = realloc(myStruct, nbr);
     
    if(myStruct == NULL)
         return 1;
     
    (*(myStruct+nbr)).surface = createSurface();
     
    return EXIT_SUCCES;
    }
    Merci d'avance

    PS: C'est pas du tout mon code, je veux dire il manque les headers, 200 lignes de codes en plus mais c'est vrai le contenu de ces lignes :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    nbr = myStruct->lifes+1;
    myStruct->lifes+1;
    myStruct = realloc(myStruct, nbr);
    Qui est important.

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    10 603
    Détails du profil
    Informations personnelles :
    Âge : 66
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 603
    Points : 17 913
    Points
    17 913
    Billets dans le blog
    2
    Par défaut
    c'est :

    myStruct = realloc(myStruct, nbr*sizeof(life));


    Et en plus en général on utilise un pointeur intermédiare, pour le cas où ça échoue... et on incrémentes QUE si ça a réussi..


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    if ( (ptr =  realloc(myStruct, nbr*sizeof(life))) != NULL )
      {
          myStruct = ptr ;
          myStruct->lifes = myStruct->lifes+1;
      }
    else
    {
       /* myStruct est inchangé */
       /* Code "error-proof" */
       nbr = nbr - 1 ; 
    }
    "Un homme sage ne croit que la moitié de ce qu’il lit. Plus sage encore, il sait laquelle".

    Consultant indépendant.
    Architecture systèmes complexes. Programmation grosses applications critiques. Ergonomie.
    C, Fortran, XWindow/Motif, Java

    Je ne réponds pas aux MP techniques

  3. #3
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    Entendu Mais je voudrais savoir si ptr était un int ou si c'est un :

    Merci d'avance

  4. #4
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Citation Envoyé par _SamSoft_ Voir le message
    Entendu Mais je voudrais savoir si ptr était un int ou si c'est un :
    Si on écrit
    forcément ptr est un pointeur et ne peut être un entier
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  5. #5
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    Je voulais parler de :


  6. #6
    Membre éprouvé
    Avatar de Shugo78
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    1 119
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 1 119
    Points : 1 001
    Points
    1 001
    Par défaut
    C'est un pointeur de type life.
    Bien que realloc retourne un pointeur générique de type void.
    Omnes Vulnerant Ultima Necat
    Bye

  7. #7
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Citation Envoyé par _SamSoft_ Voir le message
    Je voulais parler de :
    Alors, c'est un pointeur sur int, pas un int
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  8. #8
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    Merci pour tout !

  9. #9
    Membre éprouvé
    Avatar de Shugo78
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    1 119
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 1 119
    Points : 1 001
    Points
    1 001
    Par défaut
    Citation Envoyé par _SamSoft_
    Je voulais parler de :

    Code :

    int *ptr;

    Citation Envoyé par diogene
    Alors, c'est un pointeur sur int, pas un int
    Dans le bloc de code cité oui, mais dans celui de souvrion34 évoquant le passage par un autre pointeur par sécurité, non.
    Dans le code de souviron34, il s'agit d'un pointeur de type life, ou plutôt d'un pointeur générique de type void.
    Omnes Vulnerant Ultima Necat
    Bye

  10. #10
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    En fait, ca ne fonctionne pas si bien que cela et je ne comprend pas pourquoi. Voilà une "version abrégée" de mon code .

    Ca compile mais à l'exécution...

    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
     
     
    struct pos
    {
        int x;
        int y;
    };
     
    struct col
    {
        int r;
        int g;
        int b;
    };
     
    struct life
    {
        struct pos position;
        struct col color;
        int time_life;
        int activate; /* 0:non, 1:oui */
        SDL_Surface *surface;
     
    };
     
    struct world
    {
        int lifes;
    };
     
    int createNewLife(SDL_Surface* screen, struct life *myStruct, struct world *myStruct2)
    {
        if(myStruct == NULL)
            return 1;
     
        if(myStruct2 == NULL)
            return 1;
     
        int nbr = myStruct2->lifes+1;
     
        struct life *ptr = realloc(myStruct, nbr*sizeof(struct life));
     
        if(ptr == NULL)
            return 1;
     
        myStruct = ptr;
        myStruct2->lifes = myStruct2->lifes+1;
     
        int r = 0, g = 0, b = 0, x = 0, y = 0;
        int c = 254, p = L_H_CASE*L_H_CASE;
     
        SDL_Rect position;
     
        (*(myStruct+nbr)).surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
     
        (*(myStruct+nbr)).time_life = DELAY/10;
        (*(myStruct+nbr)).activate = 0;
     
        r = rand() % (c+1);
        g = rand() % (c+1);
        b = rand() % (c+1);
     
        x = rand() % (p+1);
        y = rand() % (p+1);
     
        (*(myStruct+nbr)).position.x = x;
        position.x = x;
     
        (*(myStruct+nbr)).position.y = y;
        position.y = y;
     
        while(r == 0 && g == 0 && b == 0)
        {
            r = rand() % (c+1);
            g = rand() % (c+1);
            b = rand() % (c+1);
        }
     
        (*(myStruct+nbr)).color.r = r;
        (*(myStruct+nbr)).color.g = g;
        (*(myStruct+nbr)).color.b = b;
     
        SDL_FillRect((*(myStruct+nbr)).surface, NULL, SDL_MapRGB(screen->format, r, g, b));
        SDL_BlitSurface((*(myStruct+nbr)).surface, NULL, screen, &position);
     
        return SUCCES;
    }
    PS: Le code du dessus en lui même ne compilera pas bien entendu mais ce qui est important c'est bien la fonction createNewLife et les structures.

    Merci d'avance

  11. #11
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    J'avance un peu :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    struct life *ptr = (struct life*)realloc(myStruct, (nbr*sizeof(struct life)));
    Cette ligne est elle correcte ?

    Merci d'avance

  12. #12
    Membre éprouvé
    Avatar de Shugo78
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    1 119
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 1 119
    Points : 1 001
    Points
    1 001
    Par défaut
    Oui cette ligne est correcte, ça vient de là je pense :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    [FONT=monospace]SDL_FillRect
    ((*(myStruct+nbr)).surface, NULL, SDL_MapRGB (screen->format, r, g, b));
    SDL_BlitSurface
    ((*(myStruct+nbr)).surface, NULL, screen, &position);[/FONT]
    Es-tu sur que tes variables sont correctement initialisées ?
    Omnes Vulnerant Ultima Necat
    Bye

  13. #13
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Citation Envoyé par _SamSoft_ Voir le message
    J'avance un peu :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    struct life *ptr = (struct life*)realloc(myStruct, (nbr*sizeof(struct life)));
    Cette ligne est elle correcte ?

    Merci d'avance
    Elle est correcte. Le cast est toutefois déconseillé et je préfère remplacer sizeof(struct life) par sizeof *ptr ou sizeof *myStruct, ce qui facilite la maintenance. Ainsi, on a:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    struct life *ptr = realloc(myStruct, nbr * sizeof *ptr);
    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  14. #14
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Citation Envoyé par _SamSoft_ Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
        (*(myStruct+nbr)).surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
     
        (*(myStruct+nbr)).time_life = DELAY/10;
        (*(myStruct+nbr)).activate = 0;

    La notation que tu utilises est à mon avis inutilement complexe. (*(myStruct+nbr)).surface est équivalent à myStruct[nbr].surface.

    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  15. #15
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    J'avais lu que lorsqu'on utilisait realloc, on modifiait la syntaxe. Avant que humans, ne soit un pointeur, je faisais humans[nbr] mais depuis que c'est un pointeur je fais (*(humans+nbr)) . C'est si incorrect que cela?

  16. #16
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Citation Envoyé par _SamSoft_ Voir le message
    J'avais lu que lorsqu'on utilisait realloc, on modifiait la syntaxe. Avant que humans, ne soit un pointeur, je faisais humans[nbr] mais depuis que c'est un pointeur je fais (*(humans+nbr)) . C'est si incorrect que cela?
    C'est parfaitement correct, mais c'est plus difficile à lire, pour toi et pour les autres.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (*(myStruct+nbr)).surface
    est équivanlent aux expressions suivantes:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    myStruct[nbr].surface
    (myStruct+nbr)->surface
    J'utilise volontier la première forme. Si tu désires faire resortir la sémantique pointeur de myStruct, la deuxième forme est toujours plus lisible (ce n'est que mon avis) que celle que tu utilises.

    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  17. #17
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    Merci J'ai corrigé mon code, j'utilise alors la seconde forme.

    Voilà où j'en suis concernant mon allocation (de mémoire) :

    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
     
    int createNewLife(SDL_Surface* screen, struct life *myStruct, struct world *myStruct2)
    {
        if(screen == NULL || myStruct == NULL || myStruct2 == NULL)
            return 1;
     
        int nbr = myStruct2->lifes+1;
     
        struct life *ptr = NULL;
     
        ptr = realloc(ptr, nbr * sizeof *ptr);
     
        if(ptr == NULL)
            return 1;
     
        myStruct = ptr;
     
        myStruct2->lifes++;
     
        int r = 0, g = 0, b = 0, x = 0, y = 0;
        int c = 254, p = L_H_CASE*L_H_CASE;
     
        SDL_Rect position;
     
        (myStruct+nbr)->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
     
        (myStruct+nbr)->time_life = DELAY/10;
     
        (myStruct+nbr)->activate = 0;
     
        r = rand() % (c+1);
        g = rand() % (c+1);
        b = rand() % (c+1);
     
        x = rand() % (p+1);
        y = rand() % (p+1);
     
        (myStruct+nbr)->position.x = x;
        position.x = x;
     
        (myStruct+nbr)->position.y = y;
        position.y = y;
     
        while(r == 0 && g == 0 && b == 0)
        {
            r = rand() % (c+1);
            g = rand() % (c+1);
            b = rand() % (c+1);
        }
     
        (myStruct+nbr)->color.r = r;
        (myStruct+nbr)->color.g = g;
        (myStruct+nbr)->color.b = b;
     
        SDL_FillRect((myStruct+nbr)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
        SDL_BlitSurface((myStruct+nbr)->surface, NULL, screen, &position);
     
        return SUCCES;
    }
    int createNewLife(SDL_Surface* screen, struct life *myStruct, struct world *myStruct2)
    {
        if(screen == NULL || myStruct == NULL || myStruct2 == NULL)
            return 1;
     
        int nbr = myStruct2->lifes+1;
     
        struct life *ptr = NULL;
     
        ptr = realloc(myStruct, nbr * sizeof *ptr);
     
        if(ptr == NULL)
            return 1;
     
        myStruct = ptr;
     
        myStruct2->lifes++;
     
        int r = 0, g = 0, b = 0, x = 0, y = 0;
        int c = 254, p = L_H_CASE*L_H_CASE;
     
        SDL_Rect position;
     
        (myStruct+nbr)->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
     
        (myStruct+nbr)->time_life = DELAY/10;
     
        (myStruct+nbr)->activate = 0;
     
        r = rand() % (c+1);
        g = rand() % (c+1);
        b = rand() % (c+1);
     
        x = rand() % (p+1);
        y = rand() % (p+1);
     
        (myStruct+nbr)->position.x = x;
        position.x = x;
     
        (myStruct+nbr)->position.y = y;
        position.y = y;
     
        while(r == 0 && g == 0 && b == 0)
        {
            r = rand() % (c+1);
            g = rand() % (c+1);
            b = rand() % (c+1);
        }
     
        (myStruct+nbr)->color.r = r;
        (myStruct+nbr)->color.g = g;
        (myStruct+nbr)->color.b = b;
     
        SDL_FillRect((myStruct+nbr)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
        SDL_BlitSurface((myStruct+nbr)->surface, NULL, screen, &position);
     
        return SUCCES;
    }
    A la sortie, je n'ai plus le truc avec realloc next size 0x...
    J'ai corrigé grâce à Shugo et vous (Thierry) cette erreur.

    Maintenant j'ai :

    *** glibc detected *** ./Life: malloc(): memory corruption (fast): 0x080666a8 ***
    ======= Backtrace: =========
    ....
    screen, myStruct et myStruct2 sont tout à fait valides. Les pointeurs ne sont pas égaux à NULL.

    Merci d'avance

    PS: Si je supprime l'appel à createNewLife, mon programme foncitonne super bien mais sans la gestion des naissances. J'en ai conclu que le bug est dans createNewLife, chose confirmée par un CONTROL_POINT définit comme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    #define CONTROL_POINT do { printf("%s %d, control point\n", __FILE__, __LINE__); } while (0)
    Voilà le code life.c (en entier) au cas où le calloc de départ n'est pas bon :

    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
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
     
    #include <stdio.h>
    #include <stdlib.h>
    #include "const.h"
    #include "life.h"
    #include "output.h"
     
    int createLife(SDL_Surface* screen)
    {
        struct life *humans = NULL;
        struct world myWorld;
     
        myWorld.lifes = LIFES;
        humans = calloc(myWorld.lifes, sizeof(struct life));
     
        if(humans == NULL)
        {
            fprintf(stderr, "Erreur d'allocation de la structure: humans");
            return 2;
        }
     
        char message1[32];
     
        int x = 0, y = 0, l = 254, m = L_H_CASE*L_H_CASE, n = 1, s = 5;
        int x_const = L_WINDOW+10, nbr = 0, size = 10;
        int r = 0, g = 0, b = 0, people = 0;
        int time_now = 0, time_before = 0;
        int distance1 = 0, distance2 = 0;
        int time_max = DELAY;
        int world = 1;
        int i = 0;
     
        SDL_Color colorWhite, colorBlack, colorRed, colorGreen;
        SDL_Surface *sep = NULL;
        SDL_Rect position;
     
        sep = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 480, 32, 0, 0, 0, 0);
     
        if(sep == NULL)
        {
            fprintf(stderr, "Erreur d'allocation avec la variable: sep\n");
            return 2;
        }
     
        colorWhite.r = 255;
        colorWhite.g = 255;
        colorWhite.b = 255;
     
        colorBlack.r = 0;
        colorBlack.g = 0;
        colorBlack.b = 0;
     
        colorRed.r = 255;
        colorRed.g = 0;
        colorRed.b = 0;
     
        colorGreen.r = 0;
        colorGreen.g = 255;
        colorGreen.b = 0;
     
        position.x = 0;
        position.y = 0;
     
        people = myWorld.lifes;
     
        while(people >= 0)
        {
            fprintf(stdout, "\nCreation de l'humain : %i\n", people);
     
            (humans+people)->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
     
            (humans+people)->time_life = DELAY/10;
            (humans+people)->activate = 0;
     
            r = rand() % (l+1);
            g = rand() % (l+1);
            b = rand() % (l+1);
     
            x = rand() % (m+1);
            y = rand() % (m+1);
     
            (humans+people)->position.x = x;
            position.x = x;
     
            (humans+people)->position.y = y;
            position.y = y;
     
            while(r == 0 && g == 0 && b == 0)
            {
                r = rand() % (l+1);
                g = rand() % (l+1);
                b = rand() % (l+1);
            }
     
            (humans+people)->color.r = r;
            (humans+people)->color.g = g;
            (humans+people)->color.b = b;
     
            people--;
        }
     
        people = LIFES;
     
        do
        {
            sep = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 480, 32, 0, 0, 0, 0);
     
            SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 100, 100, 100));
     
            nbr = 0;
            size = 10;
     
            writeTxt(screen, "Life par Samy Hocine", x_const, 40, size, colorBlack);
     
            writeTxt(screen, "Humains: ", x_const, 80, size, colorBlack);
     
            sprintf(message1, "%d", myWorld.lifes);
            writeTxt(screen, message1, x_const+70, 80, size, colorGreen);
     
            while(people >= 0)
            {
                time_now = SDL_GetTicks();
     
                if((humans+people)->activate == 0)
                {
                    if((humans+people)->time_life > 0)
                    {
                        if((time_now - time_before) > time_max) /* Si DELAY(ms) se sont e'coule'es */
                        {
                            time_before = time_now;
                            (humans+people)->time_life = (humans+people)->time_life-1;
                        }
     
                        i = 0;
     
                        x = rand() % (n+1);
                        y = rand() % (n+1);
     
                        distance1 = rand() % (s+1);
                        distance2 = rand() % (s+1);
     
                        if(x == 1)
                        {
                            /* deplacement vers la droite */
                            if((humans+people)->position.x < L_WINDOW-L_H_CASE)
                            {
                                (humans+people)->position.x = (humans+people)->position.x+distance1;
                                position.x = (humans+people)->position.x;
                                i++;
                            }
                            else
                            {
                                (humans+people)->position.x = L_WINDOW-L_H_CASE;
                                position.x = (humans+people)->position.x;
                                i++;
                            }
                        }
                        else
                        {
                            /* deplacement vers la gauche */
                            if((humans+people)->position.x > 0)
                            {
                                (humans+people)->position.x = (humans+people)->position.x-distance2;
                                position.x = (humans+people)->position.x;
                                i++;
                            }
                        }
     
                        if(y == 1)
                        {
                            /* deplacement vers le bas */
                            if((humans+people)->position.y < H_WINDOW-L_H_CASE)
                            {
                                (humans+people)->position.y = (humans+people)->position.y+distance1;
                                position.y = (humans+people)->position.y;
                                i++;
                            }
                            else
                            {
                                (humans+people)->position.y = H_WINDOW-L_H_CASE;
                                position.y = (humans+people)->position.y;
                                i++;
                            }
                        }
                        else
                        {
                            /* deplacement vers le haut */
                            if((humans+people)->position.y > 0)
                            {
                                (humans+people)->position.y = (humans+people)->position.y-distance2;
                                position.y = (humans+people)->position.y;
                                i++;
                            }
                        }
     
                        int start = 1, other_people = myWorld.lifes, j = 0;
     
                        while(other_people >= 0 || start == 1)
                        {
                            if((*(humans+other_people)).activate != 1) /* s'il est mort alors rien */
                            {
                                if((*(humans+other_people)).position.x != (humans+people)->position.x)
                                {
                                    start = 0;
                                }
                                else
                                {
                                    j++;
                                }
     
                                if((*(humans+other_people)).position.y != (humans+people)->position.y)
                                {
                                    start = 0;
                                }
                                else
                                {
                                    j++;
                                }
     
                                other_people--;
                            }
     
                            if(j == 2)
                            {
                                int rslt = 0;
     
                                rslt = createNewLife(screen, humans, &myWorld);
     
                                if(rslt != SUCCES)
                                {
                                    fprintf(stdout, "Erreur lors de la cre'ation d'un nouvel humain");
                                    return 3;
                                }
                            }
                        }
     
                        if(i == 2) /* si les deux positions: x et y sont ok alors */
                        {
                            r = (humans+people)->color.r;
                            g = (humans+people)->color.g;
                            b = (humans+people)->color.b;
     
                            SDL_FillRect((humans+people)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
                            SDL_BlitSurface((humans+people)->surface, NULL, screen, &position);
                        }
                    }
                    else
                    {
                        (humans+people)->activate = 1;
                        position.x = (humans+people)->position.x;
                        position.y = (humans+people)->position.y;
     
                        (humans+people)->color.r = 0;
                        (humans+people)->color.g = 0;
                        (humans+people)->color.b = 0;
     
                        r = (humans+people)->color.r;
                        g = (humans+people)->color.g;
                        b = (humans+people)->color.b;
     
                        SDL_FillRect((humans+people)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
                        SDL_BlitSurface((humans+people)->surface, NULL, screen, &position);
     
                        if(myWorld.lifes != 0)
                            myWorld.lifes--;
                    }
                }
                else
                {
                    position.x = (humans+people)->position.x;
                    position.y = (humans+people)->position.y;
     
                    (humans+people)->color.r = 0;
                    (humans+people)->color.g = 0;
                    (humans+people)->color.b = 0;
     
                    r = (humans+people)->color.r;
                    g = (humans+people)->color.g;
                    b = (humans+people)->color.b;
     
                    SDL_FillRect((humans+people)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
                    SDL_BlitSurface((humans+people)->surface, NULL, screen, &position);
                }
     
                people--;
            }
     
            position.x = 500;
            position.y = 0;
     
            SDL_FillRect(sep, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
            SDL_BlitSurface(sep, NULL, screen, &position);
     
            SDL_Flip(screen);
     
            people = LIFES;
     
            if(myWorld.lifes <= 0)
            {
                world = 0;
            }
        }
        while(world);
     
        people = LIFES;
     
        while(people >= 0)
        {
            free((humans+people)->surface);
            people--;
        }
     
        free(humans);
     
        return SUCCES;
    }
     
    int createNewLife(SDL_Surface* screen, struct life *myStruct, struct world *myStruct2)
    {
        if(screen == NULL || myStruct == NULL || myStruct2 == NULL)
            return 1;
     
        int nbr = myStruct2->lifes+1;
     
        struct life *ptr = NULL;
     
        ptr = realloc(ptr, nbr * sizeof *ptr);
     
        if(ptr == NULL)
            return 1;
     
        myStruct = ptr;
     
        myStruct2->lifes++;
     
        int r = 0, g = 0, b = 0, x = 0, y = 0;
        int c = 254, p = L_H_CASE*L_H_CASE;
     
        SDL_Rect position;
     
        (myStruct+nbr)->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
     
        (myStruct+nbr)->time_life = DELAY/10;
     
        (myStruct+nbr)->activate = 0;
     
        r = rand() % (c+1);
        g = rand() % (c+1);
        b = rand() % (c+1);
     
        x = rand() % (p+1);
        y = rand() % (p+1);
     
        (myStruct+nbr)->position.x = x;
        position.x = x;
     
        (myStruct+nbr)->position.y = y;
        position.y = y;
     
        while(r == 0 && g == 0 && b == 0)
        {
            r = rand() % (c+1);
            g = rand() % (c+1);
            b = rand() % (c+1);
        }
     
        (myStruct+nbr)->color.r = r;
        (myStruct+nbr)->color.g = g;
        (myStruct+nbr)->color.b = b;
     
        SDL_FillRect((myStruct+nbr)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
        SDL_BlitSurface((myStruct+nbr)->surface, NULL, screen, &position);
     
        return SUCCES;
    }

  18. #18
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Dans la fonction createNewLife(), tu as une fuite de mémoire (memory leak). Tout d'abord, je ne comprends pas pourquoi tu utilises realloc(NULL,...) au lieu de malloc() pour l'allocation, mais passons... Dans ta fonction createNewLife(), tu alloues l'espace mémoire pour contenir nbr structures struct life.L'adresse de la zone mémoire est affectée à myStruct (je ne trouve pas cet identificateur très parlant). Le problème, c'est que cette adresse est perdue dès la sortie de la fonction. L'espace mémoire créé dans createNewLife() est ainsi perdu et ne peut plus être libéré. N'oublie pas qu'en C, le passage des arguments à une fonction se fait par copie. Ainsi l'adresse que tu affectes à myStruct ne peut être récupérée depuis l'extérieur de la fonction. Pour retourner l'adresse de la zone alloué, on devrait grosso modo procéder comme suit:

    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
    int createNewLife(SDL_Surface* screen, struct life **pp_myStruct, struct world *myStruct2)
    {
        /*...*/
     
        /* -tc- si *pp_myStruct contient NULL, on a une allocation, si *pp_myStruct 
           contient une adresse valide, on a réallocation */
        struct life *ptr = *pp_myStruct;
        ptr = realloc(ptr, nbr * sizeof *ptr);
     
        if(ptr == NULL)
            return 1;
     
        /* -tc- retourne l'adresse contenue dans ptr à la fonction appelante*/
        *pp_myStruct = ptr;
     
        /*...*/
     
        return SUCCES;
    }
    Ta fonction createNewLife() étant une sorte de constructeurs, j'utiliserais plutôt malloc() que realloc() afin de clarifier tes intentions.

    Attention également à définir tes variables en début de bloc. La définition de variables sauvages est autorisée en C99, mais c'est pas très portable. Je recommande un code qui ressemble à (je déconseille également les return sauvages, mais c'est une question de style et beaucoup les utilisent):

    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
    int createNewLife(SDL_Surface* screen, struct life **pp_myStruct, struct world *myStruct2)
    {
        /*...*/
     
        {
            struct life *ptr = *pp_myStruct;
            ptr = realloc(ptr, nbr * sizeof *ptr);
     
            if(ptr == NULL)
                 return 1;
     
     
            *pp_myStruct = ptr;
        }
     
        /*...*/
     
        return SUCCES;
    }
    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  19. #19
    Membre averti Avatar de _SamSoft_
    Profil pro
    Étudiant
    Inscrit en
    Février 2007
    Messages
    798
    Détails du profil
    Informations personnelles :
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2007
    Messages : 798
    Points : 345
    Points
    345
    Par défaut
    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
     
    int createNewLife(SDL_Surface* screen, struct life **pp_humans, struct world *p_myWorld)
    {
        if(screen == NULL || *pp_humans == NULL || p_myWorld == NULL)
            return 1;
     
        int nbr = p_myWorld->lifes+1;
     
        struct life *ptr = *pp_humans;
     
        ptr = realloc(ptr, nbr*(sizeof *ptr));
     
        if(ptr == NULL)
            return 1;
     
        *pp_humans = ptr;
     
        p_myWorld->lifes++;
     
        int r = 0, g = 0, b = 0, x = 0, y = 0;
        int c = 254, p = L_H_CASE*L_H_CASE;
     
        SDL_Rect position;
     
        (*pp_humans+nbr)->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
     
        (*pp_humans+nbr)->time_life = DELAY/10;
     
        (*pp_humans+nbr)->activate = 0;
     
        r = rand() % (c+1);
        g = rand() % (c+1);
        b = rand() % (c+1);
     
        x = rand() % (p+1);
        y = rand() % (p+1);
     
        (*pp_humans+nbr)->position.x = x;
        position.x = x;
     
        (*pp_humans+nbr)->position.y = y;
        position.y = y;
     
        while(r == 0 && g == 0 && b == 0)
        {
            r = rand() % (c+1);
            g = rand() % (c+1);
            b = rand() % (c+1);
        }
     
        (*pp_humans+nbr)->color.r = r;
        (*pp_humans+nbr)->color.g = g;
        (*pp_humans+nbr)->color.b = b;
     
        SDL_FillRect((*pp_humans+nbr)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
        SDL_BlitSurface((*pp_humans+nbr)->surface, NULL, screen, &position);
     
        return SUCCES;
    }
    En fait, pour le nom des variables, je suis pas très inspiré (jamais inspiré).

    Je ne déclare pas à la volée.
    En réalité dans createNewLife, je pars du principe que si screen == NULL ou ... == NULL rien ne sert de créer les autres variables. C'est une perte de micro secondes ok mais c'est une perte de temps non ?

    Sinon je déclare toujours mes variables en haut dans un gros code (c.f : createLife) .

    Le code compile, s'execute mais est sûrement incorrect car il ne me donne pas le résultat attendu en gros. J'ai du me trompé dans ma syntaxe.

    Voilà l'appel à createNewLife :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    rslt = createNewLife(screen, &humans, &myWorld);
    Merci d'avance

    PS: Suis-je obligé d'utiliser un pointeur de pointeur ? Car avec un pointeur, je comprend mais un pp ...

  20. #20
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Citation Envoyé par _SamSoft_ Voir le message
    [CODE]

    PS: Suis-je obligé d'utiliser un pointeur de pointeur ?
    Si tu veux retourner l'adresse de la zone mémoire allouée dynamiquement dans le corps de ta fonction et que la valeur de retour de ta fonction est déjà utilisée pour retourner un code d'erreur, tu es obligé de passer par un paramètre de type pointeur sur pointeur sur struct life.

    Attention: si tu alloues l'espace mémoire pour nbr structures de type struct life, l'accès:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (*pp_humans+nbr)->surface
    est hors limite (comportement indéterminé)

    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Problème avec realloc
    Par darkwall_37 dans le forum Débuter
    Réponses: 28
    Dernier message: 10/12/2009, 17h37
  2. Réponses: 4
    Dernier message: 10/03/2008, 03h59
  3. Réponses: 22
    Dernier message: 29/01/2005, 11h29
  4. Re-problème avec realloc() (désolé)
    Par Yabo dans le forum C
    Réponses: 8
    Dernier message: 30/07/2003, 22h07
  5. Problème avec [b]struct[/b]
    Par Bouziane Abderraouf dans le forum CORBA
    Réponses: 2
    Dernier message: 17/07/2002, 10h25

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