- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 C 语言中的二叉树来实现内存管理模拟(伙伴)。系统工作原理概述如下:
http://en.wikipedia.org/wiki/Buddy_memory_allocation
第一次输入值时,代码工作正常,并给出所需的输出。第二次输入值时会出现此问题。我正在使用递归函数来遍历树,并且收到错误,结构存在,但结构的成员不存在。
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1 (LWP 1)]
0x00010b2c in allocate (node=0x401ba18a, reqSize=1000) at temp.c:95
95 if(node->flag == 1){
(gdb) print node
$1 = (struct block *) 0x401ba18a
(gdb) print node->flag
Cannot access memory at address 0x401ba192
相关代码发布在下面,任何帮助将不胜感激!
static int allocate(struct block* node, int reqSize) {
//BASE CASES!
printf("We've called allocate\n");
//check if the request is too small
if(reqSize < minSize){
printf("The request is too small!\n");
return -1;
}
//check if the request is too large
if(reqSize > memory){
printf("The request is too large!\n");
return -1;
}
//check if the current block is already allocated
if(node->flag == 1){
printf("This block has been allocated!\n");
return -1;
}
//var to hold returned value
int returnVal = 0;
//If the size of the request is less than or equal to half the node
if(reqSize<(node->size)/2){
printf("The size of the request is less than or equal too half the node\n");
//check if there is a left node, if not, make one and call allocate
if(node->left == NULL){
printf("There's no left node so we are making one and calling allocate with it\n");
struct block buddy1 = { .init =1.};
buddy1 = findSpace();
buddy1.init = 1;
buddy1.size = ((node->size)/2);
printf("with the size %d\n",(node->size)/2);
buddy1.flag = 0;
buddy1.parent = node;
buddy1.left = NULL;
buddy1.right = NULL;
struct block buddy2 = { .init =1.};
buddy2 = findSpace();
buddy2.init = 1;
buddy2.size = ((node->size)/2);
printf("with the size %d\n",(node->size)/2);
buddy2.flag = 0;
buddy2.parent = node;
buddy1.left = NULL;
buddy1.right = NULL;
node->left = &buddy1;
node->right = &buddy2;
returnVal = allocate(node->left,reqSize);
return returnVal;
}
//otherwise call allocate on the left node
printf("There is a left node so we are calling allocate on it\n");
returnVal = allocate(node->left,reqSize);
if(returnVal == -1){
printf("couldn't allocate a left node for some reason, so we are checking if a right node exists\n");
if(node->right ==NULL){
printf("it doesn't. We're making one and calling allocate on it!\n");
struct block buddy = { .init =1.};
buddy = findSpace();
buddy.init = 1;
buddy.size = ((node->size)/2);
printf("with the size %d\n",(node->size)/2);
buddy.flag = 0;
buddy.parent = node;
//node->left = NULL;
node->right = &buddy;
returnVal = allocate(&buddy,reqSize);
}
printf("it did, so we are calling allocate on it\n");
returnVal = allocate(node->right,reqSize);
//return returnVal;
}
return returnVal;
}
if(node->flag == 1){
return -1;
}
printf("This is the node we need!\n");
node->flag = 1;
printPostOrder(&blockArr[position]);
return 1;
}
最佳答案
您的好友节点是局部变量,在堆栈上分配,并在分配函数返回时销毁。您没有显示 block
结构或 findSpace
函数的定义,因此很难提供更多帮助。
为什么要部分初始化每个伙伴(.init
被分配一个浮点 1
),然后立即用 的返回值覆盖整个结构找到空间
?
第三个伙伴(当从左侧分配失败时为右侧)没有像其他两个伙伴一样将其左右指针初始化为NULL
。这里有很多重复的代码,最好将它们放在自己的函数中。
通常,树结构是隐式的,您只需从位于每个空闲 block 前面的结构创建一个空闲列表。合并 block 时,您可以通过将您的地址与您的大小进行异或来确定您好友的地址。您所需要的只是每个 block 一个位来告诉您伙伴是否空闲(并且具有 header 结构),如果是这样,您可以检查 header 以确保其大小相同。唯一需要的额外存储是每个最小可分配 block 1 位的位 vector ,它允许您快速确定 header 是否存在,而无需扫描空闲列表。哦,空闲列表应该是双向链接的,以允许您从中间删除元素(而不必从头开始扫描列表)。如果您愿意向分配的 block 添加 header ,则可用大小将不再是 2 的幂,但您可以避免需要外部位 vector 。
关于c - 二叉树结构实现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22508779/
我正在扩展 baseClass 监听器并尝试读取一些值,但是顺序中似乎没有任何层次结构。 我的语法的精简版如下: start: config_options+ config_options: (KEY
XML文档是一种树形结构,它从 根部 开始,然后扩展到 枝叶 一个 XML 文档范例 XML文档使用的是简单的具有自我描述性的语法 我们拿之间的小红写给小明的情书作为范例 <?xml v
要了解我想做什么,这里有一个简单的解释:我想创建模板。 一个"template"有一个“面板”这个“面板”有一些“项目”和另一个“面板”我们必须一步一步地做,直到最后一个“Item” 我想要这样的东西
我的数据结构如下。 A A1 A2 B B1 C C1 C2 C3 这些信息转移到两个名为group1和group2的表中。 group1有第一层数据和中间层数据。 gro
所以我有一个如下所示的树结构,其中我在叶节点中有一个计数属性。我想总结计数并将计数总和与其父项进行比较。比 parent1 和 parent2 有他们的 child 的总和。然后从那里继续,所以 gr
我有以下 json : var jsonObj = [ { "parentIndex": '0' ,
我正在尝试创建一个包含任意数量子类别的类别列表,其中子类别也可以有自己的子类别。 我从 Mysql 数据库中选择了所有类别,猫在标准关联数组列表中,每个类别都有一个 id、名称、parentid,如果
我想动态构建层次结构,每个节点创建为层次结构中的一个层/级别,具有自己的节点数组。这应该形成一个树结构。应该有一个根节点,以及未定义数量的节点和级别来构成层次结构大小。除了根节点之外,什么都不应该被修
我正在浏览 Wikitravel API,我注意到他们提供的 XML 文件只是将所有信息集中在一个大 blob 中。示例:http://wikitravel.org/en/Special:Export
我正在尝试设置一个UITableView来显示嵌套的线程项目。我正在使用 Core Data 和 NSFetchedResultsController,但不确定我的方法应该是什么。我想显示这样的信息:
我有一个这样的表数据库: tree{id,name,parent}content{id,content,parent}tree 表包含一个树状结构,如果 parent 是 0,它是顶级元素,如果不同,
我有一个像这样的 json 对象数组: [{ "vehicleid": 3, "name": "Teste2VDD", "brand": "Scania", "mode
我有一个 json 树结构,通过按此 fiddle 上的调用来附加该结构:http://jsfiddle.net/adrianjsfiddlenetuser/C6Ssa/4/ 按 fiddle 上的调
我需要使用 prefuse 可视化一个 xml 文件,但由于其大小,我必须一次仅加载文件的一部分,并将它们表示为 prefuse 中的树结构。我设法加载并可视化第一棵树,其中包含所有 xml 元素的深
我有一个包含如下数据的文件: ID attribute 1 'text' 101 'text' 1011 'text' 10111 'text' 1011101 'text' 1011102 'tex
这是我的数据结构: { projects: [ { revisions: [ { files: [] }, ], } ], us
我有一个列表需要转换成json格式。 这个.. var sourceList = [ { title: "item-1", indent: "0" }, { title: "item-
我希望我的内容有一个树/文件夹结构,但希望所有页面都作为一个平面 URL。例如 位于/cat1/subcat2/tulips.html 的页面将在以下位置提供: http://example.com/
简单来说 我有一个由对象组成的树结构。 是否可以构建该树并向每个对象添加对其父对象的引用? 我知道引用适用于对象,但我不确定在这种情况下是否有效? 我希望能够写出这样的东西 currentLevel
我创建了一个涉及学校学生的项目。当我开始构建这个项目时,我遇到了三个主要问题: 1)推荐或更好的 JSON 树结构? schools grades users(uids) in
我是一名优秀的程序员,十分优秀!