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

C++ Discussion :

Problème de fuite..mémoire


Sujet :

C++

  1. #1
    Membre confirmé Avatar de Flow_75
    Femme Profil pro
    Ingénieure
    Inscrit en
    Mai 2005
    Messages
    1 096
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieure
    Secteur : Transports

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 096
    Points : 633
    Points
    633
    Par défaut Problème de fuite..mémoire
    Bonjour,

    J'ai fais un programme permettant de générer un labyrinthe sous forme d'un tableau 2D.

    La fonction principale permet de generer donc le labyrinthe.

    Après avoir passé les dimensions, elle me retourne un pointeur sur un tableau 2D (int **).

    Ce tableau est alloué dans la fonction de facon dynamique.

    J'ai remarqué l'utilisation mémoire augmente à chaque appel de cette fonction..

    Je tiens quand meme à signaler qu'avant son appel, je désalloue* le tableau precedemment retourné par la meme fonction.

    Je ne vois pas où peut se trouver le probleme.

    Merci de votre aide
    F.

    *je joins le code où je desalloue le tableau tout en bas

    ci dessous le code :
    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
    #define _SCL_SECURE_NO_WARNINGS
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    #include <time.h>
    #include <algorithm>
    #include <fstream>
     
    using namespace std;
     
     
     
    int alea(int n)
        {
        int partSize   = 1 + (n == RAND_MAX ? 0 : (RAND_MAX - n) / (n + 1));
        int maxUsefull = partSize * n + (partSize-1);
        int draw;
     
        do
            {
            draw = rand();
            } while (draw > maxUsefull);
     
            return draw / partSize;
        }
     
    bool isComplete(int **tab, int x, int y)
    {
    	for(int i = 0; i < x; i++)
    	{
     
    		for(int j = 0; j < y; j++)		
    		{
    			if(tab[i][j] != 0 && tab[i][j] != -1)
    			{
    				return false;
    			}
    		}
    	}
    	return true;
    }
     
    int** generate_laby(int X, int Y)
    //int main(int argc, char **argv)
    {
     
    	int NbCaseAZero = 1;
    	int **Labyrinthe;//[(2*X)+1][(2*Y)+1];
     
    //	if(argc == 3)
    //	{
    //		X = atoi( argv[1] );
    //		Y = atoi( argv[2] );
    //	}
     
     
    	int unite = 0;
    	/* Initialisation */
     
    	srand((unsigned)time(NULL));
     
     
    	Labyrinthe = new int *[(2*X)+1];
    	fill_n(Labyrinthe,  (2*X)+1, static_cast<int*>(0));
     
    	for(int dim= 0; dim < (2*X)+1; ++dim)
    	{
    		Labyrinthe[dim] = new int[(2*Y)+1];
    	}
     
     
     
     
     
    		for(int i = 0; i <  (2*X)+1 ; i++)
    		{
    			for(int j = 0; j < (2*Y)+1; j++)
    			{
    				if(i == 0 || j == 0 || i == (2*X) || j == (2*Y) || i%2 == 0 || j%2 == 0)
    				{
    					Labyrinthe[i][j] = -1;
    				}
    				else
    				{
    					Labyrinthe[i][j] = unite++;
    				}
    			}
    		}
     
     
     
    do{
    	int x = 0;
    	int y = 0;
    	do
    	{
    		x = alea( (2*X) -2 )+1;
    		y = alea( (2*Y) -2 )+1;
     
     
    	}while( !( (x%2==1)^(y%2==1)/*(x%2 == 1 && y%2 == 0) || (x%2 == 0 && y%2 == 1)*/ ) || Labyrinthe[x][y] != -1);
     
    	int d;
    	int frompropagate, topropagate;
     
    	if( x%2 == 1)
    	{
    		d = Labyrinthe[x][y-1] - Labyrinthe[x][y+1];
     
    		if( d>0 )
    		{
    			Labyrinthe[x][y] = Labyrinthe[x][y+1];
    			frompropagate = Labyrinthe[x][y+1];
    			topropagate = Labyrinthe[x][y-1];
    		}
     
    		if( d<0 )
    		{
    			Labyrinthe[x][y] = Labyrinthe[x][y-1];
    			frompropagate = Labyrinthe[x][y-1];
    			topropagate = Labyrinthe[x][y+1];
    		}
     
    	}
     
     
    	if( y%2 == 1)
    	{
    		d = Labyrinthe[x-1][y] - Labyrinthe[x+1][y];
     
    		if( d>0 )
    		{
    			Labyrinthe[x][y] = Labyrinthe[x+1][y];
    			frompropagate = Labyrinthe[x+1][y];
    			topropagate = Labyrinthe[x-1][y];
    		}
     
    		if( d<0 )
    		{
    			Labyrinthe[x][y] = Labyrinthe[x-1][y];
    			frompropagate = Labyrinthe[x-1][y];
    			topropagate = Labyrinthe[x+1][y];
    		}
     
    	}
     
     
    	for(int i = 0; i <  (2*X)+1 ; i++)
          	{
            	for(int j = 0; j < (2*Y)+1; j++)
          		{
    			if(Labyrinthe[i][j] == topropagate)
    			{
    				Labyrinthe[i][j] = frompropagate;
    			}
     
    		}
    	}
    }
    while( !isComplete(Labyrinthe, (2*X)+1, (2*Y)+1) );
    return Labyrinthe;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    int **resultat = generate_laby(50,50);
    if(resultat != NULL)
    {
    	for(int i=0; i<50; i++) 			
            {
    		delete [] resultat[i];
    	}
     
    	delete []resultat;
    }

  2. #2
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut
    Ou je suis fatigué, ou 2*X+1 (impair) est forcément différent de 50 (pair) quelque soit la valeur de X et encore plus si X==50 (2*50+1==101).
    D'où l'utilité des choses comme std::vector, des pointeurs intelligents ou de ta propre enveloppe RAII

  3. #3
    Membre confirmé Avatar de Flow_75
    Femme Profil pro
    Ingénieure
    Inscrit en
    Mai 2005
    Messages
    1 096
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieure
    Secteur : Transports

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 096
    Points : 633
    Points
    633
    Par défaut
    Citation Envoyé par 3DArchi Voir le message
    Ou je suis fatigué, ou 2*X+1 (impair) est forcément différent de 50 (pair) quelque soit la valeur de X et encore plus si X==50 (2*50+1==101).
    D'où l'utilité des choses comme std::vector, des pointeurs intelligents ou de ta propre enveloppe RAII
    Non non, tu n'es pas fatigué, c'est moi qui suis idiot

    Merci beaucoup...

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

Discussions similaires

  1. [OpenOffice][Tableur] problème de fuites mémoires
    Par sephial dans le forum OpenOffice & LibreOffice
    Réponses: 0
    Dernier message: 23/11/2009, 17h26
  2. Problème de fuites mémoire
    Par Le Barde dans le forum C++
    Réponses: 12
    Dernier message: 02/09/2007, 08h49
  3. [VB6] Problème de fuite mémoire
    Par GyLes dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 19/03/2007, 14h58
  4. [C++] problème de fuite mémoire
    Par Cirdan Telemnar dans le forum C++
    Réponses: 26
    Dernier message: 16/06/2006, 10h16
  5. Problème de fuite mémoire sur un idFTP
    Par jeromelef dans le forum Composants VCL
    Réponses: 6
    Dernier message: 26/07/2005, 17h29

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