Reason why this C program stopped working [duplicate] - c

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.

Related

Why does memset print different value when it's set to non zero? [duplicate]

This question already has answers here:
Memset enum array values not setting correctly (C/C++)
(2 answers)
Closed 12 months ago.
I am trying to understand memset and pointer to an array. Below is my program and when I set the array contents 0 the value of *(p+6) prints 0.
But when I set the value to 5 *(p+6) it prints 84215045
Not sure whats going on.
#include <stdio.h>
#include <string.h>
int *p;
int dtk[0xA0];
int main()
{
memset (dtk, 0, 160*sizeof(dtk[0]));
p = dtk;
printf("dtk,%d",*(p+6));
return 0;
}
As Raymond Chen alluded, each byte in the 32-bit int is being set to 5, with the result being (5<<24) + (5<<16) + (5<<8) + 5 == 84215045.

Why does declaring an int array of length INT_MAX create a segmentation fault? [duplicate]

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.

Reduction in size of array when passed to a function [duplicate]

This question already has answers here:
Finding length of array inside a function [duplicate]
(7 answers)
Length of array in function argument
(9 answers)
Closed 4 years ago.
This code takes input from user and simply stores them in an array.The array is then passed to a function and then printed out.The problem is that when I check the size of array in main() and then in function() they are different.Please check the output image link I provided to clearly understand what I am saying. The size of int is 4 bytes. Dev-C++ is IDE used which uses TDM-GCC 4.9.2 64 bit compiler.Can someone clarify what is going on here?
#include<stdio.h>
int function(int formal[]);
main()
{
int input[10],loop,limit;
printf("Enter limit:");
scanf("%d",&limit);
for(loop=0;loop<limit;loop++)
{
scanf("%d",&input[loop]);
}
printf("Size of input array:%d\t",sizeof(input));
function(input);
}
int function(int formal[])
{
int loop;
printf("Size of parameter array:%d\n",sizeof(formal));
for(loop=0;loop<5;loop++)
{
printf("%d\n",formal[loop]);
}
}
output: [1]: https://i.stack.imgur.com/LqHiS.png

Segmentation fault in scanning long long int in C [duplicate]

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.

C uninitialized int has a value of 1 instead of 0 [duplicate]

This question already has answers here:
Initializing variables in C
(10 answers)
Closed 6 years ago.
#include <stdio.h>
#include <string.h>
#include "prac.h"
#define MYNAME "Butter"
int main() {
int numberOfKids;
int weight;
int shirt;
printf("If I eat a Watermelon I will weigh %d lbs \n", weight + numberOfKids+ shirt );
return 0;
}
I compiled and ran the program and the result was 1; although I expected it to be 0. When I checked the value of each variable individually, the weight variable's value was 1. Can someone explain why that specific variables result was not 0? I am new to C and want to experiment with the basics to get a deeper understanding of the nuances of C. Any help would be appreciated.
Variables inside a function in C are not guaranteed to be set to anything by default. In memory, whatever was last stored there (which might not be flushed/erased to be 0) will be what the int is initialized to.
This is answered in Initializing variables in C
EDIT: As chux has stated below, local static variables are initialized to 0 if they aren't given an initial value. Also covered Is un-initialized integer always default to 0 in c?

Resources