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:
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:
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;
} |