This question already has answers here:
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 5 years ago.
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;
}
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.
Related
This question already has answers here:
Non-static variable initialization
(7 answers)
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed last month.
I have typed out a simple program counting the number of positive divisors of a number in C, which works in CodeBlocks GCC compiler but not in the online compiler provided by the school, where it prints out some numbers close to 22k (22019 21873) etc after some inputs.
For example, for input 10 the program prints out 4 which the number of divisors, but in the online compiler it prints out 22019. I noticed when i changed the order of some conditions in the if statements from if (n<0) to if (0>n) the numbers also changed (the 22019 became 22xxx) to numbers different from the first time. Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, n, res = 0;
int i = 1;
scanf("%d", &n);
if (0>n)
printf("Wrong Input");
else {
while (n>=i)
{
res = n%i;
if (res == 0) {
c++;
i++;
}
else {
i++;
}
}
printf("%d", c);
}
return 0;
}
This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
Closed 1 year ago.
I want the output to be 6 and 4 four array A and B but I cant figure out where I made a mistake. I read a tutorial on StackOverflow where u can calculate a int array length by doing length = sizeof(array)/sizeof(array[0]), but it just doesn't work in my program. The values of sizeof(array) and sizeof(array[0]) stay constant regardless if I change the array as shown below.
#include<stdio.h>
#include<string.h>
int arraychecker(int array[])
{
int Length =sizeof(array)/sizeof(array[0]);
int arraylength = sizeof(array);
int arraylength0 = sizeof(array[0]);
printf("%d,%d,%d\n", Length, arraylength, arraylength0);
}
int main()
{
int a[]= {2,1,3,4,9,33};
int b[]={2,55,3,2};
arraychecker(a);
arraychecker(b);
return(0);
}
output: 2,8,4
2,8,4
Adding to Eugene's comment, some common practice is to also store the length in the array.
This question already has answers here:
Why does rand() yield the same sequence of numbers on every run?
(7 answers)
Closed 2 years ago.
My problem is really simple. In C, I am trying to create a set of random values to set for r, however whenever I run the code it generates the same numbers over and over again rather than a unique sequence of numbers on every iteration. How should I change the code to fix this?
My code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
int r;
for (int i;i<5;i++)
{
r=rand() % 10;
printf("%d\n",r);
}
}
This code always returns the values 1,7,4,0,9. How can I make it so that it instead randomizes each on every successive use of the function?
rand does not generate real random numbers. But to make the unique you need to seed it with something which will be different every time you run the program.
Example:
int main()
{
int r;
srand(time(NULL));
for (int i;i<5;i++)
{
r=rand() % 10;
printf("%d\n",r);
}
}
This question already has an answer here:
Segmentation Fault, large arrays
(1 answer)
Closed 4 years ago.
The following code, when compiled and run, gives me a segmentation fault. Why is this?
#include <stdio.h>
#include <limits.h>
int main(void)
{
int fat_array[INT_MAX];
return 0;
}
What you are requesting is to have about 2,147,483,647integer spaces allocated to you. Each integer is usually four bytes so that's 8,589,934,588 bytes which is 8 gigabytes of memory. This is likely above the allowed amount of memory a single process is allowed to reserve, and for good reason, so you get an error.
This question already has answers here:
Declaration of variable causes segmentation fault
(2 answers)
Closed 6 years ago.
I wonder why this program stopped working when I increase the value of array a[]
If it has, plaese tell me how to increase this value without crashing
Thanks
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i, j, save;
char a[2082001];
memset(a,'1',2082000);
for (i=2;i<=2082000;i++)
{
if (a[i]=='1')
{
save=i;
for (j=i*2;j<=2082000;j+=i)
a[j]='0';
}
}
printf("save = %d",save);
return 0;
}
Basic signed integer type. Capable of containing at least the [−32767, +32767] range; thus, it is at least 16 bits in size,
that takes j out of bounds.