- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Hash.c 根据评论进行了更新,我仍然遇到段错误。我一定漏掉了你们所说的一些东西
我已经使用 C 创建了哈希表 ADT,但是当我尝试在 ADT 中调用函数 (find_hash) 时遇到了段错误。
我已经发布了我创建的 parse.c、hash.c 和 hash.h 的所有 3 个文件,因此您可以看到所有变量。我们正在从附件中的 gettysburg.txt 文件中读取
当我调用 find_hash 时,parse.c 中发生段错误。我无法弄清楚这里发生了什么。如果您需要更多信息,我一定可以提供。
对于大量的代码,我已经完全困惑了一个星期,对此深表歉意。提前致谢
我运行程序的方式首先是: gcc -o 解析 parse.c hash.c然后:cat gettysburg.txt |解析
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "hash.h"
#define WORD_SIZE 40
#define DICTIONARY_SIZE 1000
#define TRUE 1
#define FALSE 0
void lower_case_word(char *);
void dump_dictionary(Phash_table );
/*Hash and compare functions*/
int hash_func(char *);
int cmp_func(void *, void *);
typedef struct user_data_ {
char word[WORD_SIZE];
int freq_counter;
} user_data, *Puser_data;
int main(void)
{
char c, word1[WORD_SIZE];
int char_index = 0, dictionary_size = 0, num_words = 0, i;
int total=0, largest=0;
float average = 0.0;
Phash_table t; //Pointer to main hash_table
int (*Phash_func)(char *)=NULL; //Function Pointers
int (*Pcmp_func)(void *, void *)=NULL;
Puser_data data_node; //pointer to hash table above
user_data * find;
printf("Parsing input ...\n");
Phash_func = hash_func; //Assigning Function pointers
Pcmp_func = cmp_func;
t = new_hash(1000,Phash_func,Pcmp_func);
// Read in characters until end is reached
while ((c = getchar()) != EOF) {
if ((c == ' ') || (c == ',') || (c == '.') || (c == '!') || (c == '"') ||
(c == ':') || (c == '\n')) {
// End of a word
if (char_index) {
// Word is not empty
word1[char_index] = '\0';
lower_case_word(word1);
data_node = (Puser_data)malloc(sizeof(user_data));
strcpy(data_node->word,word1);
printf("%s\n", data_node->word);
//!!!!!!SEG FAULT HERE!!!!!!
if (!((user_data *)find_hash(t, data_node->word))){ //SEG FAULT!!!!
insert_hash(t,word1,(void *)data_node);
}
char_index = 0;
num_words++;
}
} else {
// Continue assembling word
word1[char_index++] = c;
}
}
printf("There were %d words; %d unique words.\n", num_words,
dictionary_size);
dump_dictionary(t); //???
}
void lower_case_word(char *w){
int i = 0;
while (w[i] != '\0') {
w[i] = tolower(w[i]);
i++;
}
}
void dump_dictionary(Phash_table t){ //???
int i;
user_data *cur, *cur2;
stat_hash(t, &(t->total), &(t->largest), &(t->average)); //Call to stat hash
printf("Number of unique words: %d\n", t->total);
printf("Largest Bucket: %d\n", t->largest);
printf("Average Bucket: %f\n", t->average);
cur = start_hash_walk(t);
printf("%s: %d\n", cur->word, cur->freq_counter);
for (i = 0; i < t->total; i++)
cur2 = next_hash_walk(t);
printf("%s: %d\n", cur2->word, cur2->freq_counter);
}
int hash_func(char *string){
int i, sum=0, temp, index;
for(i=0; i < strlen(string);i++){
sum += (int)string[i];
}
index = sum % 1000;
return (index);
}
/*array1 and array2 point to the user defined data struct defined above*/
int cmp_func(void *array1, void *array2){
user_data *cur1= array1;
user_data *cur2= array2;//(user_data *)array2;
if(cur1->freq_counter < cur2->freq_counter){
return(-1);}
else{ if(cur1->freq_counter > cur2->freq_counter){
return(1);}
else return(0);}
}
#include "hash.h"
Phash_table new_hash (int size, int(*hash_func)(char*), int(*cmp_func)(void*, void*)){
int i;
Phash_table t;
t = (Phash_table)malloc(sizeof(hash_table)); //creates the main hash table
t->buckets = (hash_entry **)malloc(sizeof(hash_entry *)*size); //creates the hash table of "size" buckets
t->size = size; //Holds the number of buckets
t->hash_func = hash_func; //assigning the pointer to the function in the user's program
t->cmp_func = cmp_func; // " "
t->total=0;
t->largest=0;
t->average=0;
t->sorted_array = NULL;
t->index=0;
t->sort_num=0;
for(i=0;i<size;i++){ //Sets all buckets in hash table to NULL
t->buckets[i] = NULL;}
return(t);
}
void free_hash(Phash_table table){
int i;
hash_entry *cur;
for(i = 0; i<(table->size);i++){
if(table->buckets[i] != NULL){
for(cur=table->buckets[i]; cur->next != NULL; cur=cur->next){
free(cur->key); //Freeing memory for key and data
free(cur->data);
}
free(table->buckets[i]); //free the whole bucket
}}
free(table->sorted_array);
free(table);
}
void insert_hash(Phash_table table, char *key, void *data){
Phash_entry new_node; //pointer to a new node of type hash_entry
int index;
new_node = (Phash_entry)malloc(sizeof(hash_entry));
new_node->key = (char *)malloc(sizeof(char)*(strlen(key)+1)); //creates the key array based on the length of the string-based key
new_node->data = data; //stores the user's data into the node
strcpy(new_node->key,key); //copies the key into the node
//calling the hash function in the user's program
index = table->hash_func(key); //index will hold the hash table value for where the new node will be placed
table->buckets[index] = new_node; //Assigns the pointer at the index value to the new node
table->total++; //increment the total (total # of buckets)
}
void *find_hash(Phash_table table, char *key){
int i;
hash_entry *cur;
printf("Inside find_hash\n"); //REMOVE
for(i = 0;i<table->size;i++){
if(table->buckets[i]!=NULL){
for(cur = table->buckets[i]; cur->next != NULL; cur = cur->next){
if(strcmp(table->buckets[i]->key, key) == 0)
return((table->buckets[i]->data));} //returns the data to the user if the key values match
} //otherwise return NULL, if no match was found.
}
return NULL;
}
void stat_hash(Phash_table table, int *total, int *largest, float *average){
int node_num[table->size]; //creates an array, same size as table->size(# of buckets)
int i,j, count = 0;
int largest_buck = 0;
hash_entry *cur;
for(i = 0; i < table->size; i ++){
if(table->buckets[i] != NULL){
for(cur=table->buckets[i]; cur->next!=NULL; cur = cur->next){
count ++;}
node_num[i] = count;
count = 0;}
}
for(j = 0; j < table->size; j ++){
if(node_num[j] > largest_buck)
largest_buck = node_num[j];}
*total = table->total;
*largest = largest_buck;
*average = (table->total) / (table->size);
}
void *start_hash_walk(Phash_table table){
Phash_table temp = table;
int i, j, k;
hash_entry *cur; //CHANGE IF NEEDED to HASH_TABLE *
if(table->sorted_array != NULL) free(table->sorted_array);
table->sorted_array = (void**)malloc(sizeof(void*)*(table->total));
for(i = 0; i < table->total; i++){
if(table->buckets[i]!=NULL){
for(cur=table->buckets[i]; cur->next != NULL; cur=cur->next){
table->sorted_array[i] = table->buckets[i]->data;
}}
}
for(j = (table->total) - 1; j > 0; j --) {
for(k = 1; k <= j; k ++){
if(table->cmp_func(table->sorted_array[k-1], table->sorted_array[k]) == 1){
temp -> buckets[0]-> data = table->sorted_array[k-1];
table->sorted_array[k-1] = table->sorted_array[k];
table->sorted_array[k] = temp->buckets[0] -> data;
}
}
}
return table->sorted_array[table->sort_num];
}
void *next_hash_walk(Phash_table table){
table->sort_num ++;
return table->sorted_array[table->sort_num];
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct hash_entry_ { //Linked List
void *data; //Generic pointer
char *key; //String-based key value
struct hash_entry_ *next; //Self-Referencing pointer
} hash_entry, *Phash_entry;
typedef struct hash_table_ {
hash_entry **buckets; //Pointer to a pointer to a Linked List of type hash_entry
int (*hash_func)(char *);
int (*cmp_func)(void *, void *);
int size;
void **sorted_array; //Array used to sort each hash entry
int index;
int total;
int largest;
float average;
int sort_num;
} hash_table, *Phash_table;
Phash_table new_hash(int size, int (*hash_func)(char *), int (*cmp_func)(void *, void *));
void free_hash(Phash_table table);
void insert_hash(Phash_table table, char *key, void *data);
void *find_hash(Phash_table table, char *key);
void stat_hash(Phash_table table, int *total, int *largest, float *average);
void *start_hash_walk(Phash_table table);
void *next_hash_walk(Phash_table table);
Four score and seven years ago, our fathers brought forth upon this continent a new nation: conceived in liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war. . .testing whether that nation, or any nation so conceived and so dedicated. . . can long endure. We are met on a great battlefield of that war.
We have come to dedicate a portion of that field as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we cannot dedicate. . .we cannot consecrate. . . we cannot hallow this ground. The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here.
It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us. . .that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion. . . that we here highly resolve that these dead shall not have died in vain. . . that this nation, under God, shall have a new birth of freedom. . . and that government of the people. . .by the people. . .for the people. . . shall not perish from the earth.
最佳答案
这段代码的几个问题之一可能是像这样的循环:
for(table->buckets[i];
table->buckets[i]->next != NULL;
table->buckets[i] = table->buckets[i]->next)
...
for 循环的初始化部分 (table->buckets[i]
) 无效。如果 i
为 0 且 table->buckets[0] == NULL
,则此循环的条件 (table->buckets[i]->next != NULL
) 将取消引用空指针并崩溃。
这就是您的代码似乎在我的盒子上崩溃的地方,至少。当我将您的几个循环更改为:
if (table->buckets[i] != NULL) {
for(;
table->buckets[i]->next != NULL;
table->buckets[i] = table->buckets[i]->next)
...
}
...它一直在崩溃,但在不同的地方。也许这会帮助您摆脱困境?
编辑:另一个潜在的问题是那些 for 循环是破坏性的。当您调用 find_hash
时,您真的要修改所有这些桶吗?
我建议使用类似的东西:
hash_entry *cur;
// ...
if (table->buckets[i] != NULL) {
for (cur = table->buckets[i]; cur->next != NULL; cur = cur->next) {
// ...
}
}
当我这样做并注释掉您的 dump_dictionary
函数时,您的代码运行时不会崩溃。
关于c - 段。哈希表 ADT 中的错误 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10504446/
Google发布的ADT bundle 包的最终版本是什么? 由于宣布“Android Studio”为开发Android应用程序的正式IDE,因此ADT bundle 包(带有ADT插件和Andro
我在 Eclipse Indigo (3.7.2) 中更新我的 android ADT 插件时遇到了一个可怕的错误。请检查所附图片。 谢谢 最佳答案 遇到了类似的问题。这就是我修复它的方式。 1.从
我正在使用 ADT 包 22.3。这是我直接从 android.com 下载的完整离线压缩包。但现在他们已经将更新推送到 ADT bundle 22.6,所以在下载 ADT bundle 22.6 z
我用 C 语言创建了一个基于结构的抽象数据类型。在这个结构中,有指向同样基于结构的其他 ADT 的指针。我想知道如何/是否可以正确初始化内部指针并消除内存访问冲突错误。代码如下: typedef st
我想创建一棵树(使用 Node 或 ADT),其中每个节点都有一个指向其父节点的注释。下面是一个简单的链表数据结构的例子: import util::Math; import IO; import N
在 eclipse 上 - 尽管我使用的是最新的 adt 版本,但我还是收到了这个错误。我不确定它是否相关,但这发生在我安装 android-m sdk 之后。 最佳答案 从这里开始: https:/
我的应用程序运行良好。但是,一旦我从 ADT 16 更新到 ADT 17,就会收到此错误 Unable to resolve superclass of Lcom/my/app/MyActivity;
大家早上好 我是 SO 的新手,我问这个问题是因为我必须重新调整昨天的算法和编程考试。在 CodeBlocks 上输入我的考试时,我遇到了一个我无法修复的错误。基本上,考试要求从数据结构中的文件(格式
几乎没有人在 ADT 中成功导入 Apklib(aar) 引用。这是一个 issues ,但没有答案。在问题中,这是一个 solution有效但丑陋。 作为 ADT 的粉丝,我不想使用 Android
我遇到了从 安装 ADT 的问题 https://dl-ssl.google.com/android/eclipse/ 和 http://dl-ssl.google.com/android/eclip
在使用 Eclipse 开发时,尝试安装 hibernate、storm 和其他一些 ORM/DAO 生成器工具时,eclipse 停止正常工作。 我有两种错误:1 在构建它时说“运行 android
这个问题在这里已经有了答案: Update Eclipse with Android development tools v. 23 (43 个回答) 关闭8年前。 我从这个网站下载并安装了带有 an
我在创建 dynArray 并向其添加元素时遇到了问题。我不确定如何为它分配内存。我最近才开始提出建议,因此非常感谢您的帮助。 typedef struct{ doube *darray; i
假设我有一个 ADT 和类型类 Foo像这样: sealed trait A case class A1() extends A case class A2() extends A case clas
我正在使用 scala 驱动程序与 mongodb 进行 IO 操作。我的Scala版本是2.11.11 mongo db 驱动程序是 2.2.0 . 我以关于 ADT 的文档为例: sealed c
我正在尝试用 C++ 实现二叉搜索树(用于大学类(class)),但在访问我在 .h 文件中定义的结构时遇到了一些困难。 这是我的 .h 文件: class BST { protected:
我有两个空中应用程序并将它们安装在桌面上并执行它们,并且任务栏管理器中列出了两个空中进程。现在如何从另一个空中应用程序执行一个空中应用程序的某些方法? 最佳答案 使用LocalConnection .
我不是数学家,但我觉得存在一些逻辑问题。 让我们从 ADT 原语开始,例如“unit”类型。它应该在类型集的上下文中扮演“1”的角色。但事实上,我们看到“unit”类型在C、C++等中经常被称为“vo
在 C 中创建 ADT 时返回结构体指针的原因是什么?例如: typedef struct some_thing st_t; // system interface st_t* init_syste
更新到 ADT 22.3 后,出现此错误: [2013-11-09 17:56:27 - Dex Loader] Unable to execute dex: java.nio.BufferOverf
我是一名优秀的程序员,十分优秀!