に、このように、他のサイトからのコピー&ペーストを許しなさいませんそれがオリジナルのアイデアであるかどうかはわかります。私はそれを思いついたのですが、新しいものではないかもしれません)。それはWindows上で正常にコンパイルされますが、実行すると「プログラムは、Mac OSで動作しますが、Windowsの
「dlt_table.exeでエラーが発生し、終了する必要があります。
私がこの問題を抱えていた唯一の時は、病的な好奇心からヌルポインタを逆参照しようとしたときです。システムが単に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上の誰もが何かアドバイスがなかった、と彼らはここに求めてお勧めします。
Windowsでどのコンパイラを使用していますか? conio.hを使用すると、古いBorlandコンパイラが示唆されていますが、C++ Builder XEまたはGCC 4.3.3を使用してWindows上でクラッシュを再現することはできません。 –
流血のソフトウェアDev-C++ 4.9.9.2。 – Henry
ところで、ヘッダーファイルが表示されなかった理由は、コードを間違ってフォーマットしたためです.HTMLタグで囲むのではなく、コードの各行の前に4つのスペースを入れることになっています。私はあなたのためにそれを修正しました:次回は、単にあなたのコードに貼り付け、それを選択し、{}ボタンを押すことができます。 –