Home > Enterprise >  Identify error causing Segmentation fault (core dumped)
Identify error causing Segmentation fault (core dumped)

Time:11-24

I'm new to c programming. Decided I would learn by doing some of the problem sets in the cs50 open courseware. The following code produces a Segmentation fault (core dumped) error. I cannot understand why. I've read a segmentation error has to do with accessing memory you do not have access to. I don't see what would be causing that. I'm assuming it has to do with the pointers. I am new to pointers. Thank you.

    #include <stdio.h>

// https://cs50.harvard.edu/x/2021/labs/1/population/

float yearly_llamas(float starting_population) {
    // returns number of llamas at the end of the year
    float born = starting_population / 3;
    float died = starting_population / 4;
    float end_of_year_pop = starting_population   born - died;

    return end_of_year_pop;
}

int main(void) {
    
    // use floats for precision
    float *start_population;
    float *end_population;

    // set start lower limit
    int start_min = 9;

    
    // make sure input for starting population is greater than or equal to 9
    do {
        printf("Starting population: ");
        scanf("%f", start_population);
    } while (*start_population < start_min);
    
 

    // get ending population, make sure greater than or equal to the starting population
    do {
        printf("Ending population: ");
        scanf("%f", end_population);
    } while (*end_population < *start_population);

    // print for verification
    printf("%f\n", *start_population);
    printf("%f\n", *end_population);

    float end_pop = yearly_llamas(*start_population);

    printf("Llamas at the end of the year: %f\n", end_pop);


  return 0;
}

CodePudding user response:

You declared a pointer to float, but that pointer just pointed to nothing because you didn't assign address to it.

Change these lines

float *start_population;
float *end_population;

to

float f_start_population;
float f_end_population;
float *start_population = &f_start_population;
float *end_population = &f_end_population;

should solve the segmentation fault.

CodePudding user response:

While the other answer tells you the solution, I want to emphasize the way to find (and solve) this kind of problems: use a debugger. It is an important tool of a programmer, and it's best to learn to use it sooner than later. In this case, your problem is simple enough to be easily found with any debugger. Later, when you will be working with more complex code and multi-threading, it's gonna be difficult to learn to use it while you try to solve your (complex) problem. Please, try to solve this problem in your own by using a debugger.

If you are in Linux, you can use gdb and run the code until it crashes. Then, you inspect the backtrace (bt) to see the last executed line. Lastly, you define a breakpoint (p #n where #n is the line number) in the previous line of the crash, and you inspect the values (p $variable with $variable the name of your variable) and try to see why it doesn't work.

With a GUI debugger, it should be easier (for example with Visual Studio or Code::blocks).

CodePudding user response:

When you declare a pointer variable f like so float *f; you can only "use" it if the pointer is actually pointing to memory reserved by you (the term is allocated). You can either allocate the variable on the "heap" using the malloc() function, or, easier, create a separate variable on the stack (called an automatic variable) by writing float my_float; and using that. So you get:

float my_startfloat;
float *start_population = &my_startfloat;

That said, I would have only declared a float (the first) line, and then, where appropriate, used its address: &my_startfloat. For example:

 float my_startfloat;
 scanf("%f", &my_startfloat);
  • Related