This is a function in my program. With the cout statement there the program compiles and runs. If I remove the cout statement I get a segmentation fault returned. I’m using G++ compiler on linux mint. Anyone have any thoughts on this?
int findFactors(int n,int factors[],int numFactors)
{
int m=n;
int k=2;
int i;
while(m!=1)
{
for(k=2;k<=m;k++)
{
if(m%k==0)
{
factors[i]=k;
cout<<"Prime Factor: "<<factors[i]<<endl;//This is the offending statement!
factors[i++];
numFactors++;
break;
}
}
m=(m/k);
}
return numFactors;
}
iis uninitialized, so accessingfactors[i]is undefined behavior and anything can happen.Also, what is the statement
factors[i++];supposed to do?