- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是哈佛大学 CS50 中的 pset 5。它主要包括加载字典,检查所选文本中的每个单词是否在加载的字典中找到,然后卸载(释放所有分配的内存)。所有其他功能都可以工作,但是当涉及到卸载时,它只是执行,如上所述,超过 80,000,000 次释放,而在程序中只有 143,094 个 malloc,我是一个新手,所以这对我来说是令人兴奋的。下面是卸载的相关函数。
编辑 (1):以下是有关变量哈希表的一些上下文。
typedef struct node
{
char word[LENGTH+2];
struct node *next;
}
node;
node *hashtable[264636] = { NULL };
我将每个元素初始化为 NULL,以便在卸载时我可以轻松跳过哈希函数中未生成键的索引值。
//LOAD FUNCTION: Loads the dictionary into a hash table. Djb2 function used.
bool load(const char *dictionary)
{
head = malloc(sizeof(node));
head->next = NULL;
if (head == NULL)
{
unload();
return false;
}
opntr = fopen(dictionary, "r");
while (fscanf(opntr, "%s", WORD) != EOF)
{
wnode = malloc(sizeof(node));
if (wnode == NULL)
{
unload();
return false;
}
strcpy(wnode->word, WORD);
wnode->next = head;
head = wnode;
unsigned long key = hash(wnode->word);
hashtable[key] = wnode;
wnode = wnode->next;
}
return true;
}
// Checks whether the input word is somewhere within the dictionary
bool check(const char *word)
{
char dword[strlen(word) + 1];
strcpy(dword, word);
for (int c = 0; c < strlen(dword); c++)
{
dword[c] = tolower(dword[c]);
}
int key_w;
key_w = hash(dword);
node *cursor = hashtable[key_w];
while (cursor != NULL)
{
if (strcmp(cursor->word, dword) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Unloads memory allocated (?) to store the dictionary
bool unload(void)
{
for (int in = 0; in < 264636; in++)
{
node *fhead = hashtable[in];
while (fhead != NULL)
{
node *fcursor = fhead->next;
free(fhead);
fhead = fcursor;
}
}
return true;
}
最佳答案
如果有人觉得它有帮助,问题出在加载函数内。分配的节点被错误地更新,因此散列表数组的元素不是独立的链表,并且每次我们想要添加节点时,我都不必要地使用头节点来指向列表的第一个元素。相反,为了将新节点添加到哈希表数组中的相应元素,我们使用该元素本身作为链表的头,并对其进行排列,使其指向添加的每个节点,从而成功地跟踪链表的开头链接列表。哈希表中的每个元素最初都指向 NULL,以便我们可以找到每个链表的末尾。
int null()
{
for (int i = 0; i < 264636; i++)
{
hashtable[i]->next = NULL;
}
return 0;
}
bool load(const char *dictionary)
{
opntr = fopen(dictionary, "r");
while (fscanf(opntr, "%s", WORD) != EOF)
{
wnode = malloc(sizeof(node));
if (wnode == NULL)
{
unload();
return false;
}
strcpy(wnode->word, WORD);
unsigned long key = hash(wnode->word);
wnode->next = hashtable[key];
hashtable[key] = wnode;
}
return true;
}
关于CS50 拼写器 : Unload executes 80, 000,000+ 免费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55928086/
----- 简而言之----- 我有一个文件,比如x.html,它加载到其他文件的一些div中(使用函数jquery.load)。 但是x.html需要知道是否在您的位置加载了另一个html。 那么,
美好的一天, 我有java web应用程序,我会在登录后执行类似以下操作: 登录后,系统将显示登陆页面,在登陆页面中,会有一个选项卡菜单,并且选项卡菜单中有许多模块可以触发以导航到不同的页面。 我需要
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: how to delete the pluginassembly after AppDomain.Unload(do
我想在用户使用 jQuery 放弃特定页面时执行操作方法。 该页面有以下代码: $(window).unload(function () { aler
我加载了两个模块(NecessaryModule1.hs 和 NecessaryModule2.hs,如 Haskell : loading ALL files in current director
当用户单击提交按钮并执行操作时,我需要使用 VBA 关闭 Excel 用户窗体。 如何关闭用户表单本身? 我已经尝试过此操作,但它返回 361 错误。 Unload Me 最佳答案 正如最上面的答案所
我尝试运行以下查询以从 Redshift 卸载到 S3,但收到无益的错误。我们尝试转义所有可能导致问题的字符,并传入参数(包装程序是Python): unload ('select
我无法在 SO 上找到使用 Java VM (JVM) 参数来记录类卸载的 Web 搜索工作方式。 这里http://www.herongyang.com/JVM/ClassLoader-JVM-Op
我正在使用 NodeJS-ReactJS 同构应用程序,当我单击 Link 时,我收到一条错误消息 未捕获( promise )错误:请求已终止 可能原因:网络不在线、Access-Control-A
我有以下代码: D $(window).unload( function () { alert("Bye now!"); } );
我正在阅读 jQuery API Documentation 中给出的函数 .unload() 的 jQuery 文档。 上面写得很清楚,.unload()将在1.8之后的版本中被弃用,并在3.x中被
我们一直遇到一个奇怪的问题,我不确定如何解决,我认为这可能与最近的 Google Chrome 更新有关,但我想要一些方法在错误跟踪器上打开问题之前检查自己的健全性。 问题 我们有一个内部网络应用程序
我正在使用 PHP + MySQL + JavaScript 制作一个小型聊天应用程序,我编写了一个函数 disonnectUser(),当用户按下断开连接按钮时调用该函数。在这里: function
我继承了一个 web 框架,以前的开发人员在页面生命周期的 init/unload 方法中打开和关闭了他的数据库连接。构造函数本质上是这样的(为了说明重点而简化); public class Base
我需要在 c#/.net 中实现一个插件架构以便加载 自定义用户定义的操作 自定义数据网格的数据类型处理代码/转换/... 来自非静态链接程序集文件。 因为应用程序必须处理许多自定义用户定义的操作,所
XNA 游戏有一个 Unload() 方法,其中的内容应该被卸载。但这有什么意义呢?如果正在卸载所有内容,那么游戏肯定正在退出,在这种情况下所有内容都会被垃圾收集,对吧? 最佳答案 据我所知,它对任何
根据多种资源(例如,通过 C# 的 MSDN 和 CLR),当我们调用 AppDomain.Unload(userDomain) 时,userDomain 中的线程将被强制抛出 ThreadAbort
到目前为止的故事是这样的,我有一个使用 AppDomain 来执行某些任务的 worker thingy。该域的设置和拆卸成本很高。所以我为工作人员创建了一个 WeakReference 对象的每个线
我正在尝试在卸载页面之前向服务器发帖,然后我关注了 this它工作正常。我的问题是 window.unload 上的 $.post 在它卸载后 被触发。我尝试使用注销链接并检查我的日志,我得到以下信息
任何人都可以解释为什么下面的行会在运行时出现在输出控制台中? (一个可能的答案是完全 permGen,但这可以排除,因为该程序仅使用 PermGen 中可用的 max100MB 中的 24MB) [卸
我是一名优秀的程序员,十分优秀!