c - 程序适用于 Mac OS,但不适用于 Windows

标签 c

请原谅从另一个网站复制粘贴,比如这个:

我写了一个程序将数据存储在一个表中,像链表一样链接(不知道这是不是一个原创的想法——我自己想出来的,但它可能不是一个新的)。它在 Windows 上编译成功,但当我运行它时,它只是说,

"dlt_table.exe has encountered an error and needs to close."

我遇到这个问题的唯一一次是有一次我出于病态的好奇心试图取消对空指针的引用。意识到系统可能只是从 malloc() 返回空指针,我尝试检查错误。还是什么都没有。

然后,我在我的 Mac 上试了一下。它构建没有问题,而且更好的是,我想出的算法确实有效!但是我仍然对为什么它不能在 Windows 上工作感到困惑。这是代码:

#include <stdio.h>
//#include <conio.h> (Windows only- taking it out for Mac dev)
#include <stddef.h>
#include <stdlib.h>

/*############################################################################*/

/*Node structure-- this is what the table is built off.*/
typedef struct dlt_node {
        int value;
        struct dlt_node *left, *right, *up, *down;
        } dlt_node_t;

/*############################################################################*/

/*Function prototypes-- visible at bottom of file.*/        
dlt_node_t *make_table(void);
int len_table(dlt_node_t *bucket);
int parse_table(dlt_node_t *bucket);

/*############################################################################*/

main()
{
      dlt_node_t *bucket = make_table();

int table_size = len_table(bucket);
      printf("Table size: %i\n", table_size);

parse_table(bucket);

//getch();
      return 0;
      }

/*############################################################################*/

/*Make a table, and return the table's bucket.*/
dlt_node_t *make_table(void)
{
           /*Allocate structures.*/
           dlt_node_t *bucket = (struct dlt_node*) malloc(sizeof(struct dlt_node));
           dlt_node_t *node1 = (struct dlt_node*) malloc(sizeof(struct dlt_node));
           dlt_node_t *node2 = (struct dlt_node*) malloc(sizeof(struct dlt_node));
           dlt_node_t *node3 = (struct dlt_node*) malloc(sizeof(struct dlt_node));

/*Check for NULL's.*/
           if(bucket == NULL || node1 == NULL || node2 == NULL || node3 == NULL){
                     printf("ERR: ENOMEM.\n");
        //             getch();
                     return;
                     }

/*Assign values, then pointers to other members of the table*/
           bucket->value = 1;
           node1->value = 2;
           node2->value = 3;
           node3->value = 4;

bucket->left = NULL;
           bucket->right = node1;
           bucket->up = NULL;
           bucket->down = node2;

node1->left = bucket;
           node1->right = NULL;
           node1->up = NULL;
           node1->down = node3;

node2->left = NULL;
           node2->right = node3;
           node2->up = bucket;
           node2->down = NULL;

node3->left = node2;
           node3->right = NULL;
           node3->up = node1;
           node3->down = NULL;

/*Return the table's bucket.*/
           return bucket;
           }

/*Find the number of nodes in the table. Skewed if nodes are randomly deleted.*/
int len_table(dlt_node_t *bucket)
{
    dlt_node_t *probe_x, *probe_y;
    int x = 0, y = 0;

for(probe_y; probe_y != NULL; probe_y = probe_y->right) y++;

for(probe_x;probe_x!=NULL;probe_x = probe_x->right) x++;

return x*y;
    }

/*Parse the table, print values.*/
int parse_table(dlt_node_t *bucket){
    dlt_node_t *current, *current_row = bucket;

for ( current_row; current_row!=NULL; current_row = current_row->down ){
        current = current_row;
        for ( current; current != NULL; current = current->right ){
                            printf( "Current value: %i\n", current->value );
                             }
        }
    }

抱歉复制粘贴,Lockergnome.net 上没有人有任何建议,他们建议在这里询问。

最佳答案

在您的函数 len_table 中,您尚未初始化指针 probe_xprobe_y。 (它们在 c 中未默认初始化为 NULL)。因此,在循环的第一次迭代中,您的程序很可能会崩溃。

关于c - 程序适用于 Mac OS,但不适用于 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4999155/

相关文章:

c - 何时在 C 的嵌套结构中使用指向另一个结构的指针

c - 在 C 中循环用户输入并反转字符数组?

c - 当我尝试计算学生平均成绩时,程序停止工作

c - C中 `sizeof`的用法

c - 在 C 中使用位运算对 float 进行向下舍入

c - printk 格式中 %pa[p] 中 p 的含义

c - ld : cannot find crt1. o:没有这样的文件或目录

c - if语句在主函数中被忽略

c - 如何测量 Linux 上 C 语言的执行时间

从 bootloader 调用 32 位或 64 位程序