简体   繁体   English

为什么在c中调用函数时程序崩溃?

[英]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 . 我最近不得不创建一个程序,其中用户输入某个整数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. 之后,我的int main()必须调用一个单独的函数int getNextFibonacciNumber(void) ,该函数计算斐波纳契序列中的第N个项,然后进行打印。 When I compile the code, Vode::Blocks says that there aren't any errors or warnings. 当我编译代码时,Vode :: Blocks表示没有任何错误或警告。 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. 程序崩溃时,它说:filename.exe已停止工作。 A problem caused the program to stop working correctly. 问题导致程序停止正常运行。 Windows will close the program and notify you if a solutions is available. Windows将关闭程序并通知您是否有解决方案。 However, when the code is compiled in Code::Blocks, everything is fine . 但是,当代码在Code :: Blocks中编译时, 一切都很好

#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. N未初始化,但用作C99可变长度数组大小说明符。 Reading a value from a uninitialized variable invokes UB. 从未初始化的变量读取值将调用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 . 但是,还有另一个问题,即在getNextFibonacciNumber具有相同的UB导致构造。 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. 同样,输入N的数字不会“传达”给getNextFibonacciNumber ,因此我高度怀疑该程序是否可以工作,即使它没有崩溃也是如此。

Code::Blocks (or rather the compiler Code::Blocks calls) only checks if you have written "legal" c code. Code :: Blocks(或更确切地说是编译器的Code :: Blocks调用)仅检查您是否编写了“合法” c代码。 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]; int N,斐波那契[N];

I guess you want to create an integer N and an array of the same size. 我猜您想创建一个整数N和一个相同大小的数组。 However right now you create an integer N (that has some "random" value, presumably 0) and an array of the FIXED size N. 但是,现在您创建一个整数N(具有一些“随机”值,大概为0)和一个FIXED大小为N的数组。

If you change N late on in your program this does not affect the size of your array "fibonacci" in any way. 如果您稍后在程序中更改N,则不会以任何方式影响数组“ fibonacci”的大小。 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. 因此,如果在程序开始时N偶然是0,那么您已经创建了一个大小为0的数组。即使您从控制台输入中读取了一个值(例如5)。 Trying to read and write to this array causes problems. 尝试读写此数组会导致问题。

Moving the part 移动零件

int fibonacci[N]; int fibonacci [N];

below your "scanf" line will fix this problem. 在您的“ scanf”行下方将解决此问题。 At this point N is initialized (and not some random number). 此时,N被初始化(而不是随机数)。

Also be aware that the variable N in the main function 还要注意,主函数中的变量N

int main() int main()

has no connection at all to the N variable in your function 函数中的N变量完全没有连接

int getNextFibonacciNumber() int getNextFibonacciNumber()

The second N is a newly created variable (again set to some "random" value). 第二个N是新创建的变量(再次设置为某个“随机”值)。 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) int getNextFibonacciNumber(int N)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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