0

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++;
        }
    }
}     
3
  • 2
    Your factors function says it returns a pointer to int, but it doesn't return anything. Hint: compile with -Wall. Commented Sep 9, 2015 at 22:22
  • For future reference valgrind is a massively useful tool for locating seg-faults. The usage is as simple as valgrind ./program and (if the program was compiled with the -g flag) will tell you the exact line the program faulted on and give a stack trace. It also finds memory leaks which is helpful. Commented Sep 9, 2015 at 22:24
  • You could start by always compiling with all the warnings enabled. then fix those warnings. In this case, fixing the warnings will most likely also fix the problem Commented Sep 10, 2015 at 6:24

2 Answers 2

3

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I don’t know why I didn’t see that! And gcc wasnt showing errors before, good to know.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.