0

I'm trying to write a simple program to calculate the number of common factors for two numbers.I am stuck with segmentation fault(core dumped) occurring while scanning the second number.I don't understand where is the fault?

#include <stdio.h>
#include <stdlib.h>

int main()
{
long long int first,second,t,k;
long long int i,count=0;
scanf("%lld",&first);
scanf("%lld",&second);
//storing the lowest of two numbers in t  
if(first<second){
    t=first;
}
else{
    t=second;
}
//initialising an array to be used as flags
int com[t];
for(i=0;i<t;i=i+1){
    com[i]=1;
}
for(i=0;i<t;i=i+1){
    if(com[i]==1){
        if(first%(i+1)==0&&second%(i+1)==0){
            count=count+1;
        }
        else{
            for(k=2;k*(i+1)-1<t;k=k+1){
                com[k*(i+1)-1]=0;
            }
        }
    }
}
printf("%lld\n",count);
 return 0;
}
Vishal
  • 33
  • 1
  • 5
  • This isn't where your error is ([case and point](http://ideone.com/XrgEvb)). The undefined behavior is elsewhere. Check the return value of `scanf` and it's kin, always! – StoryTeller - Unslander Monica Apr 05 '17 at 08:09
  • What input are you providing? How did you confirm that the segmentation fault occurred in the scanning process and not after it? – David Schwartz Apr 05 '17 at 08:12
  • I checked my code by putting printf statements throughout and confirmed that it does indeed stop at the second scanf statement. – Vishal Apr 05 '17 at 08:13
  • @Vishal So a `printf` followed by an `fflush` after the second `scanf` did not print? Can you edit your code to show that? (We can't debug code we can't see. And you don't know that *this* code faulted in the `scanf`.) – David Schwartz Apr 05 '17 at 08:13
  • @Vishal - printf statement don't confirm anything. If output is buffered (as stdout often is) you won't see any printout near the actual crash site. – StoryTeller - Unslander Monica Apr 05 '17 at 08:14
  • 1
    @DavidSchwartz the second scanf doesn't work if the number is more than 6 digits. specifically you can try this 123456789 9876543 – Vishal Apr 05 '17 at 08:15
  • @DavidSchwartz the code you see here is all the code I'm working on, there is nothing else. – Vishal Apr 05 '17 at 08:16
  • @Vishal Okay, so you don't know that it's the second `scanf` that's faulting in that code. And that's the only code you're working on. Thank you. – David Schwartz Apr 05 '17 at 08:17
  • @StoryTeller You are right, I checked the return value of scanf and it is 1 for both the scanf statements, so the error is somewhere else. Thanks for pointing out. Can you tell me what do you mean by output is buffered? – Vishal Apr 05 '17 at 08:21
  • 1
    This is a stack overflow, which occurs here `int com[t];` when you try to allocate far too much memory on the stack. – Lundin Apr 05 '17 at 08:25
  • @Lundin Gotit, thanks for pointing out. – Vishal Apr 05 '17 at 08:31
  • @Vishal - Buffered output means that what ever you print doesn't appear in the terminal right away. Instead it's stored in a buffer by the call. The way, IO operations (which are somewhat costly) can be reduced. When the buffer is full, the entire content is printed at once. – StoryTeller - Unslander Monica Apr 05 '17 at 09:10

1 Answers1

3

I suspect your input is a truly big number (edit: you confirmed it in a comment). The call stack is fairly limited in size, and declaring a huge variable length array can easily overflow it.

Replace int com[t]; with the following:

int *com = malloc(sizeof *com * t);

And don't forget to free it, of course, when you are done.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458