I would like to list all permutations of n numbers. Until now everything seems normal but I encountered a very strange behaviour. With this code:
int **liste_permutations(int n){
int i, fact = factorielle(n);
int **tab=malloc(sizeof(int*)*fact);
for(i=0; i<fact; ++i)
{
tab[i] = malloc(sizeof(int)*n);
}
for(i=0;i<n;++i)
{
tab[0][i] = n-i;
}
for(i=1;i<fact;++i)
{
tab[i] = next_permutation(tab[i-1], n);
printf(" ");
}
return tab;}
The output of this main()
int **tab;
tab = liste_permutations(3);
for(i=0; i<factorielle(3); ++i)
{
for(j=0; j<3; ++j)
{
printf("%d", tab[i][j]);
}
printf("\n");
}
is
321
231
213
312
132
123
but if I change it to
int **liste_permutations(int n){
int i, fact = factorielle(n);
int **tab=malloc(sizeof(int*)*fact);
for(i=0; i<fact; ++i)
{
tab[i] = malloc(sizeof(int)*n);
}
for(i=0;i<n;++i)
{
tab[0][i] = n-i;
}
for(i=1;i<fact;++i)
{
tab[i] = next_permutation(tab[i-1], n);
}
return tab;}
the output of the main is :
321
231
321
231
321
231
And if I try to do this with n=5 for exemple, the output is blank (probably because it try to output 125 " ")
here is the next_permutation code :
int *next_permutation(int *t, int n){
//printf("n = %d\n", n);
int i, max, count;
for(i=0;(i<n) && (max !=i); ++i)
{
if(t[i] == n)
{
max = i;
}
if(t[i] == (t[i-1]+1))
{
++count;
if(count == (n-1))
{
return NULL;
}
}
}
//printf("max = %d\n", max);
if(n==1)
{
//printf("n=1\n");
return NULL;
}
int *next = malloc(n);
if(max == n-1)
{
//printf("max == n-1\n");
int *s;
s = malloc(sizeof(int));
for(i=0; i<(n-1);++i)
{
s[i]=t[i];
}
for(i=0; i<n-1; ++i)
{
//printf("%d", s[i]);
}
//printf("\n");
s = next_permutation(s, n-1);
if(s == NULL)
{
//printf("NUUUUUUl");
// next = NULL;
return NULL;
}
//printf("reprise en n = %d\n", n);
for(i=1;i<n;++i)
{
next[i] = s[i-1];
}
//printf("\n");
free(s);
next[0]=n;
return next;
}
else
{
//printf("max != n-1\n");
for(i=0; i<n; ++i)
{
next[i] = t[i];
}
int tmp = next[max];
next[max] = next[max+1];
next[max+1] = tmp;
for(i=0;i<n;++i)
{
//printf("%d", next[i]);
}
//printf("\n");
return next;
}}
EDIT : modified what the 2 first comment said, but I still have the sam issue.
EDIT2 : Thank you to every body who helped me ! espescially to mweerden who showed me the right path (it was because count was uninitialized) !
next_permutationcode is leaking memory right and left, not to mention all the initial allocations inliste_permutations: they are also gone after re-assignment. Rather than allocating a new array each time, you should pass an existing array for the result, i.e.next_permutation(tab[i], tab[i-1], n)