fonction et allocation mémoire
bonjour,
je viens d’écrire deux fonction la première dans un fichier HBMOLS.c qui contient un include pour un fichier FSP.h et HBMOLS.h et un deuxième FSP.c qui contient un include pour FSP.h et HBMOLS.h
le code du fichier HBMOLS.c est comme suit:
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
| int dim;
int problem_size;
int nombrObjPris;
int nombrObjNonPris;
ind* create_ind(int dim)
/* Allocates memory for one individual. */
{
ind *p_ind;
assert(dim >= 0);
assert(problem_size >=0 );
assert(nombrObjPris >=0 );
assert(nombrObjNonPris >=0 );
p_ind = (ind*) chk_malloc(sizeof(ind));
p_ind->objPris = (int*) chk_malloc(nombrObjPris * sizeof(int));
p_ind->objNonPris = (int*) chk_malloc(nombrObjNonPris * sizeof(int));
p_ind->fitness = -1.0;
p_ind->explored = 0;
p_ind->f = (double*) chk_malloc(dim * sizeof(double));
p_ind->d = (int*) chk_malloc(problem_size * sizeof(int));
return (p_ind);
} |
le fichier FSP.c
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
| void evalMOKP(ind *x)
{
int capa[dimension],i,j; int const dimension, NBITEMS;
int nombrObjPris; int nombrObjNonPris;
for(i=0; i<dimension; i++){
capa[i]=0; x->f[i]=0;
for(j=0; j<NBITEMS; j++)
nombrObjPris=0; nombrObjNonPris=0;
{
if (capa[i]+ weights[i][x->d[j]]< capacities[i])
{
capa[i] += weights[i][x->d[j]];
x->f[i] += profits[i][x->d[j]];
x->objPris[nombrObjPris]=x->d[j]; nombrObjPris++;
}
else if (capa[i]+ weights[i][x->d[j]] > capacities[i])
{x->objNonPris[nombrObjNonPris]=x->d[j]; nombrObjNonPris++;}
}
}
} |
le fichier FSP.h contient le prtotype de la fonction evalMOKP et le fichier HBMOLS.h contient la structure ind .
Code:
1 2 3 4 5 6 7 8 9 10
| typedef struct {
int *objPris;
int *objNonPris;
float fitness;
int explored;
double* f;
int* d;
}ind; |
mon problème est dans la fonction eval MOKP, la taille de X->objPris[] et x->objNonPris[] est inconnu, je veux a chaque fois que la condition if est vrai il met l’élément dans le tableau X->objPris[] et si la condition est fausse il met l’élément dans x->objNonPris[]. certes mon code est faux mais j'arrive pas à trouver la bonne solution. si quelqu’un a une petite idée je serais reconnaissante.