简体   繁体   English

turbo c 3.0 DOS box中程序异常终止

[英]Abnormal program termination in turbo c 3.0 DOS box

I am getting an error Abnormal program termination when I execute this code.执行此代码时出现错误异常程序终止。

The objective the program is read array of numbers from file list.txt and perform recursive binary and recursive linear search on that loaded array.程序的目标是从文件list.txt读取数字数组,并对该加载的数组执行递归二进制和递归线性搜索。

Here is my code:这是我的代码:

#include <stdio.h>
#include <conio.h>

void menu();

int a[30000],n;

void main()
{
    FILE *fp;
    int i, ch;

    fp = fopen("list.txt", "r");
    if(fp == NULL)
    {
        printf("\nCant read\n");
        exit(0);
    }

    for(i = 0; i < n; i++)
        fscanf(fp, "%d", &a[i]);

    fclose(fp);

    for(i = 0; i < n; i++)
        printf(" %d ", a[i]);

    menu();

    scanf("%d", &ch);
    if(ch == 1)
    {
        printf("ch1\n");
    }
    else if(ch == 2)
    {
        printf("ch2\n");
    }
    else
    {
        exit(1);
    }
}//end main



void menu()
{
    printf("\nEnter the number of elements in array\n");
    scanf("%d", &n);
    printf("\n1.Linear Search\n2.Binary Search\n3.Exit\nEnter your choice\n");
}

I have the logic for my choice 1 and 2. I need to know whats wrong with my above code.我有我选择 1 和 2 的逻辑。我需要知道我上面的代码有什么问题。 Please help me out in this请帮我解决这个问题

Turbo C!涡轮C! That brings back some memories, but I don't have my copy any more.这勾起了我的一些回忆,但我再也没有我的副本了。 Anyway, you're using n before you initialize it in menu() .无论如何,在menu()初始化它之前,您正在使用n (As the commenters pointed out.) You might also consider allocating your array with malloc() instead of a fixed size. (正如评论者指出的那样。)您还可以考虑使用malloc()而不是固定大小来分配数组。

Borland Turbo C came with Turbo Debugger, and in fact I think Borland gave it away for free for a while, so running in that can give you an idea where the abnormal termination happened and a stack trace you can use to check the variables that caused it. Borland Turbo C 附带了 Turbo Debugger,实际上我认为 Borland 免费赠送了一段时间,因此运行它可以让您了解异常终止发生的位置以及可以用来检查导致异常终止的堆栈跟踪它。 Another good practice is to put in assert() calls to make sure your assumptions about what's in your variables are true.另一个好的做法是调用assert()以确保您对变量中的内容的假设是正确的。

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

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