In this C program
Code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define p 200
#define l 10
char **ab;
int noofpalindrom=0;
//check palindrome//
int palindrome(char *c)
{
int i,k;
k=strlen(c);
for(i=0;i<k;i++)
{
if(c[i]!=c[k-i-1]) return 0;
}
return 1;
}
//unique palindrome//
int unique(char *a)
{
int i;
for (i=0;i<noofpalindrom;i++)
if(!strcmp(ab[i],a)) return 0;
return 1;
}
//print i length palnindrome from string b
int f1(int i, char *b, char c)
{
if(i>=0 && b[i]==c ) return 1;
return 0;
}
void f(char *b,int n)
{
int i,j,k=0,m=0;
char *d;
d=(char *)malloc(sizeof(char)*(n+1));
k=strlen(b);
for(i=0;i<k-n+1;i++)
{
for(j=i;j<i+n;j++)
{
d[m]=b[j];
m++;
}
m=0;
d[n]='\0';
if(palindrome(d) && unique(d) && f1(i-3,b,'a') && f1(i-2,b,'b') && f1(i-1,b,'a'))
{
strcpy(ab[noofpalindrom],d);
noofpalindrom++;
}
}
free(d);
// for(i=0;i<noofpalindrom;i++) printf("%s\n",ab[i]);
}
//thue morse extension//
void thuemorsemorphism(char *c)
{
int i,k,j,m;
for(i=1;i<=l;i++)
{
k=strlen(c);
m=k;
for(j=0;j<k;j++)
{
if(c[j]=='a') c[m]='b';
if(c[j]=='b') c[m]='a';
m++;
}
}
}
int main()
{
int p1=1,i,length,length1;
char *a;
length1=pow(2,l-1);
length=pow(2,l);
a=(char *)malloc(sizeof(char)*(length+1));
a[0]='a';
a[1]='\0';
ab=(char **)malloc(sizeof(char *)*length1);
for(i=0;i<length1;i++) ab[i]=(char *)malloc(sizeof(char)*length1);
thuemorsemorphism(a);
while(p1<p)
{
f(a,p1);
p1=p1+1;
}
free(a);
for(i=0;i<length1;i++) free(ab[i]);
free(ab);
printf("\n");
return 0;
}
after compilation, when I type
[CODE]
Code:
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes --track-origins=yes ./ms2
it gives
Code:
Conditional jump or move depends on uninitialised value(s)
==2936== at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2936== by 0x40093C: thuemorsemorphism (ms2.c:66)
==2936== by 0x400A54: main (ms2.c:88)
==2936== Uninitialised value was created by a heap allocation
==2936== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2936== by 0x4009E0: main (ms2.c:83)
==2936==
==2936== Conditional jump or move depends on uninitialised value(s)
==2936== at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2936== by 0x4007DA: f (ms2.c:40)
==2936== by 0x400A67: main (ms2.c:91)
==2936== Uninitialised value was created by a heap allocation
==2936== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2936== by 0x4009E0: main (ms2.c:83)
I am not able to resolv this error. It shows error on line
Code:
a=(char *)malloc(sizeof(char)*(length+1));
This program I am not able to run for p=200. But it runs successfully for l=100.