简体   繁体   中英

Why does the program crash when the function is called in c?

I recently had to create a program where the user enters a certain integer N . After that, my int main() had to call a seperate function, int getNextFibonacciNumber(void) , which calculates the N th term in the fibonacci sequence and then prints it. When I compile the code, Vode::Blocks says that there aren't any errors or warnings. This said, when I try to run the program, it automatically crashes. I have read it and re-read it but I am not able to see where it all went wrong. Would anyone be able to shed some light on this mysery? Thank you very much! When the program crashes, it says: filename.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solutions is available. However, when the code is compiled in Code::Blocks, everything is fine .

#include <stdio.h>
#include <stdlib.h>

int getNextFibonacciNumber(void);

int main()
{
    int N, fibonacci[N];

    printf("Enter a positive integer:");
    scanf("%d", &N);
    printf("The %d th term in the Fibonacci sequence is: %d", N, getNextFibonacciNumber());
}

int getNextFibonacciNumber()
{
    int N, i, fibonacci[N];

    fibonacci[0] = 0;
    fibonacci[1] = 1;
    for(i = 2; i < N+1; i++)
    {
        fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
    }
    return(fibonacci[N-1]);
}

The problem is, that this

int main()
{
    int N, fibonacci[N];

invokes undefined behavior. N is not initialized, but used as a C99 variable length array size specifier. Reading a value from a uninitialized variable invokes UB.

To fix this issue you have to write

int main()
{
    int N;

    printf("Enter a positive integer:");
    scanf("%d", &N);

    int fibonacci[N];

However there's another problem, namely that you have the same UB causing construct in getNextFibonacciNumber . You have to fix that to. Also the number entered into N is not "communicated" to getNextFibonacciNumber , so I highly doubt that this program worked at all, even if it didn't crash.

Code::Blocks (or rather the compiler Code::Blocks calls) only checks if you have written "legal" c code. It does not (and can not) check if your program does what you want, if your program will exit at any point (or simply run forever), if your program causes errors and crashs and stuff like this.

When you say

int N, fibonacci[N];

I guess you want to create an integer N and an array of the same size. However right now you create an integer N (that has some "random" value, presumably 0) and an array of the FIXED size N.

If you change N late on in your program this does not affect the size of your array "fibonacci" in any way. So if your N was by chance 0 at the beginning of your program than you have created an array of size 0. Even if you read a value (say 5) from the console input. Trying to read and write to this array causes problems.

Moving the part

int fibonacci[N];

below your "scanf" line will fix this problem. At this point N is initialized (and not some random number).

Also be aware that the variable N in the main function

int main()

has no connection at all to the N variable in your function

int getNextFibonacciNumber()

The second N is a newly created variable (again set to some "random" value). If you want to pass data from one function to another you should do it by passing it as an argument in brackets:

int getNextFibonacciNumber( int N)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM