Прошу прощения за копирование-вставку с другого сайта, как этот:
Я написал программу для хранения данных в таблице, связанной как связанный список (не знаю, оригинальная ли это идея - я сам придумал ее, но она может быть не новой) Он успешно компилируется в 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 не дал никаких советов, и они рекомендовали спросить здесь.