Segmentation Fault in C due to pointer - c

I have recently started coding in C, and am doing some stuff on project Euler. This is my code for challenge three so far. The only problem is when I run the compiled code it throws a segmentation fault. I think it may be due to a pointer I called, the suspect pointer is underneath my comment. I did some research into the subject but I cant seem to be able to fix the error. Any advice?
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool is_prime(int k);
int * factors(int num);
int main(){
int input;
while (true){
printf("Enter a number to get the prime factorization of: ");
scanf("%d", &input);
if (is_prime(input) == true){
printf("That number is already prime!");
}else{
break;
}
}
//This is the pointer I think is causing the problem
int * var = factors(input);
int k;
for (k = 0; k < 12; k++){
printf("%d", var[k]);
}
}
bool is_prime(int k){
int i;
double half = ceil(k / 2);
for (i = 2; i <= half; i++){
if (((int)(k) % i) == 0){
return false;
break;
}
}
return true;
}
int * factors(int num){
int xi;
static int array[1000];
int increment = 0;
for (xi = 1;xi < ceil(num / 2); xi++){
if (num % xi == 0){
array[increment] = xi;
increment++;
}
}
}

The factors function has no return statement. It's supposed to return a pointer but it doesn't return anything.
Side note: Enable your compiler's warnings (e.g., with gcc -Wall -Wextra). If they're already enabled don't ignore them!

Your function is declared as
int * factors(int num);
but it's definition doesn't return anything and yet you are using it's return value in assignment. This triggers undefined behavior. It will compile if compiled without rigorous warnings and the return value will most likely be whatever random value happened to be left in the return register (e.g. EAX on x86).
C-99 Standard § 6.9.1/12 Function definitions
If the } that terminates a function is reached, and the value of the
function call is used by the caller, the behavior is undefined.

Related

C program giving incorrect output

I am writing a C program to sum up prime numbers below a certain limit (9 for now). I expect 17 but the compiler gave me an unexpected output of 32781.
#include <stdio.h>
#include <stdbool.h>
bool isprime(int n);
int main(){
const int LIMIT=9;
int sum;
for (int j=1;j<LIMIT;j++){
if (isprime(j)){
sum+=j;
}
}
printf("%d",sum);
return 0;
}
bool isprime(int n){
if (n<=1){
return false;
}
else{
for(int i=2;i<n;i++){
if (n%i==0){
return false;
break;
}
}
return true;
}
}
Does anyone understand why this happened?
You declarred int sum; but didn't give sum a starting value, so it's basically reading garbage from memory. In c you need to initialize your variables properly. int sum = 0; should fix the problem.
If you are using clang as your compiler, compiling using -Wall should warn you about this.
Local variables are not initialized, so you need to initialize at declaration or before use.
int sum = 0;
or...
int sum;
for (sum = 0; bla; bla)
If the variable had been declared globally (outside of any function... main is a function) it will automatically initialize to 0.
#include <stdio.h>
int a;
int main(void)
{
int b;
printf("%d\n%d\n", a, b);
return 0;
}
Variable 'a' will be 0 and 'b' will be garbage because it's an uninitialized local variable.

What exactly is the difference between blocks and loops?

Code-1: No warnings - No errors ... everything works fine
#include <stdio.h>
int main()
{
int r = 1;
printf("using %d\n", r);
for (int k = 1; k <= 2; k++)
{
int r = r * 2;
}
return 0;
}
Code-2: Wrong
#include <stdio.h>
int main()
{
int cnt = 1;
printf("using %d\n", cnt);
{
int cnt = cnt * 2;
}
return 0;
}
compiler response:
'cnt' is used uninitialized in this function [-Werror=uninitialized]
int cnt = cnt * 2;
So, I understand there is some difference between the loop and block in this case, but I am unable to figure out. Can anyone tell me how the scope of a variable works here?
They both have exactly the same problem i.e. r and cnt are self-initialized in respective programs.
This is potentially undefined because of use of uninitialized variables (which has indeterminate value) if they happen to have trap representation.
gcc happens to detect it one case and doesn't in the other case. gcc has -Wuninitialized -Winit-self options but it still doesn't detect the first case even with these options. Regardless, the issue remains (and the same) in both.

C - can variate location be promoted?

I am new to the language and was trying a simple code. I wanted to try to create a loop based on pointers. but it seems like you can not promote variate location like in assembler. or i just did it wrong? and if i really can't can i force a new variadate to be born in specific location? that was my code
#include <stdio.h>
int main(void) {
int firstnumber = 1;
int *beginning = &firstnumber;
printf("%i %i \n",firstnumber,beginning);
Test1(firstnumber,beginning);
return 0;
}
Test1 (int num, int begin)
{
int reserve = num;
if(num != 100)
{
&num +=2;
num = (reserve+1);
return Test1(num, begin);
}
else
{
int assist = begin;
while(*assist != 100)
{
printf("/n \n %i %i \n /n",num,assist);
&assist += 2;
}
}
}
I know it might look ridiculous but i'm really curious
You're thinking about this backwards. Pointers are the variables you can move around, so use them for anything you want to move around.
firstnumber is an integer variable - the compiler decides where it is stored and you can't tell the compiler to rebind the name firstnumber to a different location. You can, however, move a pointer around as much as you like. So,
void Test1(int num) {
&num +=2;
num = 42;
}
is nonsense, but
void Test2(int *num) {
num += 2;
*num = 42;
}
is fine - so long as num+2 is still a valid allocated object. For example, you could call it like
int i[5];
Test2(i); /* sets i[2] = 42 */
(if you pass in an array of fewer than 3 integers you get a runtime bug rather than a compile error, as Test2 damages your stack frame or other memory it shouldn't be touching).
No, you cannot promote/change variable location.
So this:
int num;
&num +=2;
does not make sense, and will result in an error:
error: lvalue required as left operand of assignment
&num +=2;
^~
Same for:
int assist;
&assist += 2;
Your code will compile only if you modify these statements (but now the logic is changed, you should work on that), but now it will result in an infinite loop, since your function never returns in the else case:
#include <stdio.h>
void Test1 (int num, int* begin);
int main(void) {
int firstnumber = 1;
int *beginning = &firstnumber;
printf("%i %i \n",firstnumber, *beginning);
Test1(firstnumber, beginning);
return 0;
}
void Test1 (int num, int* begin)
{
int reserve = num;
if(num != 100)
{
num +=2;
num = reserve + 1;
return Test1(num, begin);
}
else
{
int assist = *begin;
while(assist != 100)
{
printf("/n \n %i %i \n /n",num,assist);
assist += 2;
}
}
}
Good luck!
OK i checked the code deeper and got several conclusions:
function variable can not inherit his ancestor adress. therefore using a function for the task is useless unless i use some static variable
I had another conclusion but i don't remember what it was. sorry and thank you for your time

Strange error (the program works right only if i print)

#include <stdio.h>
#include <stdlib.h>
int factor(int n, int k, int v[], int *check){
int i=0;
if (n==1) {
*check=1;
return 1;
}
while (i<k && *check!=1){
if (n%v[i]==0) factor(n/v[i], k, v, check);
i++;
}
if (*check==1) return 1;
else return 0;
}
int fac(int n, int k, int v[]){
int check;
return factor(n,k,v,&check);
}
int main(){
int i,k, temp;
int P[2]={2,3};
int A[100];
for (i=1; i<20;i++) A[i]=0;
for (i=1; i<20;i++) {
temp=fac(i,2,P);
if(temp==1) {/*printf("%d ", temp);*/ A[i]=6; }
}
printf("\n");
for (i=1; i<20;i++) printf("%d ", A[i]);
return 1;
}
This function finds the integers <20 that can be expressed as the product of 2 and 3. Factor and fac check if the number can be expressed in that way; in the main i create a vector A in this way: A[i]=0 if i cannot be expressed in that way, A[i]=6 otherwise. The problem is that if i use the commented line the program works right, but if i don't use it, A will be made of only 6s. How is that possible (i think it's corrleated with the pointer argument in factor function, but i'm not sure)?
Problems like this usually mean that there is undefined behaviour somewhere. In order to spot the problem early, always compile with warnings enabled; e.g. if you use GCC, always provide at least -Wall flag (and -O3 often helps to get more warnings, since compiler analyzes the code more thoroughly).
E.g. this is the output when I compile your code:
gcc -Wall -O3 my.c
my.c: In function ‘fac’:
my.c:19:7: warning: ‘check’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int check;
^
So, as it points out, the variable check is left uninitialized. I'm afraid that your intention was to always initialize it with this snippet:
if (n==1) {
*check=1;
return 1;
}
But it doesn't work this way: the next time fac is called, there is a new local variable check, and it's left uninitialized.
I'm not sure the logic in your code is correct, but fixing this undefined behaviour should eliminate the "strange" error which you observe.

C Recursion program won't compile w/ GCC

#include <stdio.h>
int main (void)
{
int n, x;
int factorial (int n)
{
if (x<=0)
{
printf("x equals: ");
return 1;
}
else
{
return n * factorial (n-1);
}
f(x)=f(x-1)+2; //says error is here
}
return 0;
}
I've tried some things and can't get it to work. I could just be overtired and looking past the smallest thing but help would be much appreciated! Thanks :)
You cannot declare a function definition inside of main() or any other function ... function definitions have to be stand-alone and cannot have embedded function definitions inside of them.
Also I'm not sure what you're doing on the line that you've marked as an error since f() is not a defined function, so you can't call it. Furthermore, it would need to return some type of l-value, such as a pointer to a static variable declared inside the function, or a pointer passed by reference to the function and even then the syntax is not right since there would be a required dereference ... so basically you can't do what you're doing on that line.
To get something that compiles, try
#include <stdio.h>
int factorial (int n)
{
if (n <= 0)
{
return 1;
}
else
{
return n * factorial (n-1);
}
}
int main (void)
{
int x;
x = factorial(5);
printf("Factorial of 5 is equal to %d", x);
return 0;
}
Use indentation to see possible problems with scope:
#include <stdio.h>
int main (void)
{
int n, x;
int factorial (int n)
{
if (x<=0)
{
printf("x equals: ");
return 1;
}
else
{
return n * factorial (n-1);
}
f(x)=f(x-1)+2; //says error is here
}
return 0;
}
As far as I can remember, C doesn't have closures.
A function cannot be defined inside another function. However gcc allows it as an extension. You have defined a function named factorial but are trying to use f which hasn't been declared anywhere.

Resources