c - 为什么这个 print 语句可以阻止 C 程序崩溃?

标签 c struct crash printf depth-first-search

我有以下程序,它首先存储一些城市之间的直飞航类,然后使用DFS查询两个城市是否有间接航类连接。

程序在查询步骤中不断崩溃,因此我尝试使用 print 语句来查找问题,奇怪的是,当执行搜索的函数中有 print 语句时,程序不会崩溃。代码有什么问题?

(我正在使用代码:Blocks 13.12)

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

#define MIN_SIZE 2
#define MAX_SIZE 10000
#define MIN_CON 1
#define FALSE 0
#define TRUE 1

//global variables
struct city** checked;
int* num_connect;
int n, found=0;

//Define a struct
struct city
{
    //Declaration of struct members
    int name;
    struct city **connected; //array of all cities with direct flights to
};

int readCity(int n)
{
    //Declaration of a variable
    int city;

    do
    {
        scanf("%d", &city);
    }while(city<MIN_CON && city>n);
    return city;
}

void addFlight(struct city *list, int orig, int dest)
{
    //decl
    int i=0;
    int j=0;

    //check if orig is in list
    while(num_connect[i]!=0 && list[i].name!=orig)
    {
        i++;
    }

    //if it isnt add it
    if (num_connect[i]==0)
    {
        list[i].name =orig;
        list[i].connected = malloc((num_connect[i]+1)*sizeof(struct city*));
    }
    else
    {
         //reallocate memory to store additional flight connection
        list[i].connected = realloc(list[i].connected, (num_connect[i]+1)*sizeof(struct city*));
    }

    num_connect[i]++;

    //check if dest is in list
    while(num_connect[j]!=0 && list[j].name!=dest)
    {
        j++;
    }

    //if it isnt add it
    if (num_connect[j]==0)
    {
        list[j].name =dest;
        list[j].connected = malloc((num_connect[j]+1)*sizeof(struct city*));
    }
    else
    {
         //reallocate memory to store additional flight connection
        list[j].connected = realloc(list[j].connected, (num_connect[j]+1)*sizeof(struct city*));
    }

    num_connect[j]++;

    //add b to a's connected and add b to a's connected
    list[j].connected[num_connect[j]-1]=&list[i];
    list[i].connected[num_connect[i]-1]=&list[j];

    printf("JUST CONNECTED %d WITH %d\n", list[i].name, list[j].name);
}

int inChecked(struct city* c)
{
    int i;

    while(checked[i]!=c && i<n)
    {
        i++;
    }

    if (i==n)
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

void search_connection(struct city *list, int orig, int dest)
{
    //decl
    int i=0, k=0, j=0, p=0;

    printf("   ");  // <------------------------------------------------------------

    //Find origin city in list
    while(i<n && list[i].name!=orig)
    {
        i++;
    }

    //add to checked
    while(checked[k]!=NULL)
    {
        k++;
    }
    checked[k]=&list[i];

    //Check for 'dest' city in connected of origin
    while(j<num_connect[i] && list[i].connected[j]->name!=dest)
    {
        j++;
    }

    //If-statement to determine if dest was found
    if (j!=num_connect[i])
    {
        //Set 'found' to 1
        found=1;
        return;
    }
    else
    {
        //While not all connected have been checked and not found
        while(p<num_connect[i] && found==0)
        {
            if (!inChecked(list[i].connected[p]))
            {
                //call method on it
                search_connection(list, list[i].connected[p]->name, dest);
            }
            p++;
        }
    }
}

int main()
{
    //Declaration of variables
    int i, m;
    int city_a, city_b, q_result;

    //Read input
    do
    {
        //printf("Enter number of cities:\n");
        scanf("%d", &n);
    }while(n<MIN_SIZE || n>MAX_SIZE);

    //Declare an array of cities
    struct city* cities;

    //Allocate memory for array of 'n' cities
    cities = malloc(n*sizeof(struct city)); // <---------------- FREE later!!!

    //Allocate memory for array of 'n' pointers to cities
    checked = calloc(n,sizeof(struct city*)); // <---------- FREE later!!!

    //Allocate memory for array of 'n' integers
    num_connect = calloc(n,sizeof(int)); // <------------ FREE later!!!

    //Read input
    do
    {
        //printf("Enter number of connections:\n");
        scanf("%d", &m);
    }while(n<MIN_SIZE || n>MAX_SIZE);

    //For-loop to read connected cities
    for (i=0; i<m; i++)
    {
        //Read two cities
        city_a = readCity(n);
        city_b = readCity(n);

        //add flight connecting the two cities
        addFlight(cities, city_a, city_b);
    }

    //Read connection to query
    city_a = readCity(n);
    city_b = readCity(n);

    //Search for connection between the two cities by in-direct flight
    search_connection(cities, city_a, city_b);

    //Print results
    if (found==1)
    {
        printf("Yes");
    }
    else
    {
        printf("No");
    }

    //Free up memory
    //  TO DO. . .

    return 0;
}

最佳答案

inChecked()中,您永远不会初始化i,因此您的函数将随机运行。如果该函数返回 false 的次数过多,稍后您的 checked 数组可能会溢出。

关于c - 为什么这个 print 语句可以阻止 C 程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29067941/

相关文章:

c - 结构中的枚举; c的新手

c - 可以用 C 改变屏幕亮度吗?

inheritance - 一个结构是否可以扩展现有结构,同时保留所有字段?

.net - Windows XP 和 7 之间发生了什么变化导致我的 Mono C# 应用程序崩溃?

c++ - 让 GNU 使用不同的编译器

objective-c - 将 C 与 Objective C 混合

c - 将文件中的单词加载到 C 中的数组中

c++ - 返回带有模板化类的类成员结构

solr - 30 秒后 Riak 开始崩溃

ios - 在 Xcode 5.1.1 中打开 Storyboard后屏幕变黑