S
sylsau
Hi,
I am doing a little program who calculates the permutation of a set of
vertex.
I use the recursivity for this calcul.
My function who calculate the permutations :
void permutation(set *e, int *current, int nbre)
{
set *p, *copie;
int *tmp;
int i;
if(e != NULL)
{
p = e;
tmp = realloc(current, sizeof *current *(nbre+1));
if(tmp == NULL) exit(-1);
else current = tmp;
while(p != NULL)
{
copie = copie_elt(e); // the function copie_elt let to copy the
set e and to return this copy
current[nbre] = p->st;
delete_elt(&copie,p->st); // this function let to delete an
element of copie. The element who
// is associated with the value p->st
permutation(copie,current,nbre+1); // call of permutation with the
set reducted.
p = p->suivant;
}
}else{
// In current, we have stocked the current permutation,
so we print it
for(i=0; i<nbre; i++)
printf("%d - ",current);
printf("\n");
}
}
I call it in the main with :
/* initialization of the variable e */
permutation(set,current,0);
By executing the program, I have this on my screen (I tried to debug
with GDB) :
(gdb) run
3 - 1 - 2 - 4 -
*** glibc detected *** double free or corruption (fasttop): 0x0804a048
***
Program received signal SIGABRT, Aborted.
0x4004ea27 in raise () from /lib/tls/libc.so.6
(gdb)
So, I tried this function with a char * for the variable current, the
code gives it :
void permutation(set *e, char *current, int nbre)
{
set *p, *copie;
char *tmp;
int i;
if(e != NULL)
{
p = e;
tmp = realloc(current, sizeof *current *(nbre+1));
if(tmp == NULL) exit(-1);
else current = tmp;
while(p != NULL)
{
copie = copie_elt(e); // the function copie_elt let to copy the
set e and to return this copy
current[nbre] = p->st;
delete_elt(&copie,p->st); // this function let to delete an
element of copie. The element who
// is associated with the value p->st
permutation(copie,current,nbre+1); // call of permutation with the
set reducted.
p = p->suivant;
}
}else{
// In current, we have stocked the current permutation,
so we print it
for(i=0; i<nbre; i++)
printf("%d - ",current);
printf("\n");
}
}
I call it in the main with :
permutation(e,current,0);
And there, the program gives the waited answer :
(gdb) run
3 - 1 - 2 - 4 -
3 - 1 - 4 - 2 -
3 - 2 - 1 - 4 -
3 - 2 - 4 - 1 -
3 - 4 - 1 - 2 -
3 - 4 - 2 - 1 -
1 - 3 - 2 - 4 -
1 - 3 - 4 - 2 -
1 - 2 - 3 - 4 -
1 - 2 - 4 - 3 -
1 - 4 - 3 - 2 -
1 - 4 - 2 - 3 -
2 - 3 - 1 - 4 -
2 - 3 - 4 - 1 -
2 - 1 - 3 - 4 -
2 - 1 - 4 - 3 -
2 - 4 - 3 - 1 -
2 - 4 - 1 - 3 -
4 - 3 - 1 - 2 -
4 - 3 - 2 - 1 -
4 - 1 - 3 - 2 -
4 - 1 - 2 - 3 -
4 - 2 - 3 - 1 -
4 - 2 - 1 - 3 -
Program exited normally.
(gdb)
Here, the program gives all the permutations for the set = {1,2,3,4}.
It's good but the only thing that I changed between the two functions
is the int * who becam a char *.
So, I don't know what is the probleme in the first version of
permutation with the int *.
Someone would have an idea of the problem who cause this error ?
Thanks for your help.
Sylvain.
I am doing a little program who calculates the permutation of a set of
vertex.
I use the recursivity for this calcul.
My function who calculate the permutations :
void permutation(set *e, int *current, int nbre)
{
set *p, *copie;
int *tmp;
int i;
if(e != NULL)
{
p = e;
tmp = realloc(current, sizeof *current *(nbre+1));
if(tmp == NULL) exit(-1);
else current = tmp;
while(p != NULL)
{
copie = copie_elt(e); // the function copie_elt let to copy the
set e and to return this copy
current[nbre] = p->st;
delete_elt(&copie,p->st); // this function let to delete an
element of copie. The element who
// is associated with the value p->st
permutation(copie,current,nbre+1); // call of permutation with the
set reducted.
p = p->suivant;
}
}else{
// In current, we have stocked the current permutation,
so we print it
for(i=0; i<nbre; i++)
printf("%d - ",current);
printf("\n");
}
}
I call it in the main with :
/* initialization of the variable e */
permutation(set,current,0);
By executing the program, I have this on my screen (I tried to debug
with GDB) :
(gdb) run
3 - 1 - 2 - 4 -
*** glibc detected *** double free or corruption (fasttop): 0x0804a048
***
Program received signal SIGABRT, Aborted.
0x4004ea27 in raise () from /lib/tls/libc.so.6
(gdb)
So, I tried this function with a char * for the variable current, the
code gives it :
void permutation(set *e, char *current, int nbre)
{
set *p, *copie;
char *tmp;
int i;
if(e != NULL)
{
p = e;
tmp = realloc(current, sizeof *current *(nbre+1));
if(tmp == NULL) exit(-1);
else current = tmp;
while(p != NULL)
{
copie = copie_elt(e); // the function copie_elt let to copy the
set e and to return this copy
current[nbre] = p->st;
delete_elt(&copie,p->st); // this function let to delete an
element of copie. The element who
// is associated with the value p->st
permutation(copie,current,nbre+1); // call of permutation with the
set reducted.
p = p->suivant;
}
}else{
// In current, we have stocked the current permutation,
so we print it
for(i=0; i<nbre; i++)
printf("%d - ",current);
printf("\n");
}
}
I call it in the main with :
permutation(e,current,0);
And there, the program gives the waited answer :
(gdb) run
3 - 1 - 2 - 4 -
3 - 1 - 4 - 2 -
3 - 2 - 1 - 4 -
3 - 2 - 4 - 1 -
3 - 4 - 1 - 2 -
3 - 4 - 2 - 1 -
1 - 3 - 2 - 4 -
1 - 3 - 4 - 2 -
1 - 2 - 3 - 4 -
1 - 2 - 4 - 3 -
1 - 4 - 3 - 2 -
1 - 4 - 2 - 3 -
2 - 3 - 1 - 4 -
2 - 3 - 4 - 1 -
2 - 1 - 3 - 4 -
2 - 1 - 4 - 3 -
2 - 4 - 3 - 1 -
2 - 4 - 1 - 3 -
4 - 3 - 1 - 2 -
4 - 3 - 2 - 1 -
4 - 1 - 3 - 2 -
4 - 1 - 2 - 3 -
4 - 2 - 3 - 1 -
4 - 2 - 1 - 3 -
Program exited normally.
(gdb)
Here, the program gives all the permutations for the set = {1,2,3,4}.
It's good but the only thing that I changed between the two functions
is the int * who becam a char *.
So, I don't know what is the probleme in the first version of
permutation with the int *.
Someone would have an idea of the problem who cause this error ?
Thanks for your help.
Sylvain.