- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序涉及在队列中组合树列表,我的程序编译完美并且适用于大约一半的代码然后向我抛出这个错误。我已经将它缩小到涉及我的 freeTree 以及我如何在编码文件中调用它,对于一些背景故事,这是基于文本文件中字符频率的编码器,所以如果有一个包含 5 个不同字符的文本文件,那么这段代码将从队列中的 5 个不同节点开始。队列中的每个初始节点都包含字符和频率,优先级队列基于频率,以决胜局作为字符的数值。
malloc_printerr (action=3, str=0x7ffff7b98938 "double free or corruption (fasttop)", ptr=0x6034b0) at malloc.c:5027
结构体是这样的,符号代表字符,权重代表频率
struct treeNode
{
unsigned char symbol;
unsigned long weight;
struct treeNode* left;
struct treeNode* right;
struct treeNode* next;
};
struct queue
{
struct treeNode* first;
struct treeNode* last;
};
这是我的 freeTree 方法
void freeTree( struct treeNode* root )
{
if( root != NULL )
{
freeTree( root->left );
freeTree( root->right );
free( root );
}
}
这些是我在代码的主要部分中使用的变量或树节点
struct treeNode* newNode;
struct treeNode* holder;
这就是所有被调用的地方,它应该通过我的队列将两个节点组合成一棵树,顶部的节点等于两个输入节点的权重,这应该继续直到队列只包含一棵大树
while( treeCount > 1 )
{
newNode = combineTwo( q->first, q->first->next );
if( charCount == 2 )
{
holder = q->first;
q->first = q->first->next;
freeTree( holder );
holder = q->first;
q->first = newNode;
freeTree( holder );
}
else
{
holder = q->first;
q->first = q->first->next;
freeTree( holder );
holder = q->first;
q->first = q->first->next;
freeTree( holder );
insertSortedNode( q, newNode );
}
charCount--;
holder = NULL;
}
这是我的 combineTwo 方法,它将使两个输入节点成为一个节点上的叶子
struct treeNode* combineTwo( struct treeNode* first, struct treeNode* second )
{
struct treeNode* newNode = createNode( '\0', (first -> weight) + (second -> weight));
if( first -> weight > second -> weight )
{
newNode -> right = first;
newNode -> left = second;
}
if( first -> weight < second -> weight )
{
newNode -> left = first;
newNode -> right = second;
}
if( first -> weight == second -> weight )
{
if( first -> symbol > second -> symbol )
{
newNode -> right = first;
newNode -> left = second;
}
if( first -> symbol < second -> symbol )
{
newNode -> left = first;
newNode -> right = second;
}
}
return newNode;
}
如果需要更多信息,我会整天在这里回答任何问题
最佳答案
combineTwo
创建一个新节点,它(可能)将 newnode
的 left
和 right
指向 first
和 second
参数值。然后,您的 while 循环中的代码使用 freeTree
释放这些指针。
例如:
newNode = combineTwo( q->first, q->first->next ); // newNode->left = q->first (possibly)
if( charCount == 2 )
{
holder = q->first;
q->first = q->first->next;
freeTree( holder ); // freeing the original q->first here. newNode still points to it.
需要判断新节点的left和right是应该指向原来的树节点,还是应该删除它们(原来的节点)。如果没有无效指针,您就无法同时执行这两项操作。
关于c - 在我的编码器中双重释放或损坏(fasttop),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530461/
我正在清除以前的 malloc 中的内存,并且收到此错误:“./prot”中的错误:双重释放或损坏(fasttop)。我为某些结构分配了内存,但在尝试释放它们时遇到了此错误。这是我使用的结构: str
我写了下面的一段代码,得到了上面提到的错误 谁能告诉我哪里错了 谢谢。 #include "iostream" #include "sstream" #include int main() {
我的程序涉及在队列中组合树列表,我的程序编译完美并且适用于大约一半的代码然后向我抛出这个错误。我已经将它缩小到涉及我的 freeTree 以及我如何在编码文件中调用它,对于一些背景故事,这是基于文本文
大家好,我有一个像这样的数组大小调整函数 int add_time_element (time_t *array, time_t element, size_t *size) { if (NU
我有以下类,我在其中强制编译器生成所有复制/移动构造函数和赋值运算符。 class foo { public: float *x; size_t size; foo(int m
我看到了这个错误: *** glibc detected *** perl: double free or corruption (fasttop): 0x0000000003c1daa0 *** 错
我的代码的以下部分在执行 * glibc detected ./a.out: double free or corruption (fasttop): 0x08e065d0 时给我这个消息** 我已经
我在下面的代码中收到了双重免费或损坏(fasttop)。似乎无法弄清楚哪个指针正在创建此错误。看起来错误是在 echo_cnt 函数的 while 循环中产生的。谢谢! static void
所以我正在使用可调整大小的数组实现堆,每次重新分配内存时我都会收到此错误。问题在于 realloc ..我只是不明白它出了什么问题。这是插入函数: void* insert (data_t *data
我要使用ast_malloc和ast_realloc函数拼接一个长字符串。当我加载这个模块时,会发生段错误。有什么问题吗?非常感谢!Asterisk 的输出如下: *** glibc detected
我刚遇到一个奇怪的问题。我将在下面粘贴完整的代码: #include #include #include using namespace std; // declare a base c
以下方法在运行时生成双重释放或损坏 (fasttop) 错误(在第一行)。它是随机发生的,即它可能在程序运行的第一秒或第 35 秒发生。 QSharedPointer Utils::loadImg(Q
我的程序得到以下输出: $ ./list Enter list 1: [1,2,3,4] [ Enter list 2: [2,5,8,0] [ [1,2,3,4] [1,2,3,4] ***
对 QByteArray 的清除调用会产生以下异常: * 检测到 glibc */home/yan/FPS2/FPS2: double free or corruption (fasttop): 0
我实现了一个 Stack 类并尝试利用该类来解决 Hanoi 问题。这是我的代码: template class Node { public: Node() {
我正在尝试动态分配一个数组来从命令行读取用户输入。它可以工作 99/100 次,但如果我重复输入一堆字符,有时会出现段错误错误或双重释放或损坏(fasttop)错误。此错误相对难以重现。 我很确定错误
我想编写简单的智能指针,但遇到了问题。这只是代码的一部分,但我在这里发现了问题: #include using namespace std; class Number{ public:
基于 valgrind,我相信我的错误源于这些人,因为错误发生在我将一个新集分配给另一个集之后。 Set Z -> A^B(返回 Set 的交集运算)。我只是不确定我做错了什么,任何帮助将不胜感激!
我想知道为什么下面的程序在运行时会出现“double free or corruption (fasttop)”的错误。我知道我可以使用字符串而不是字符数组。但我想使用具有动态内存分配的字符数组。你能
我在运行 Linux 的远程服务器上使用 httr 授权谷歌分析 API。到目前为止,该代码一直运行完美,而且它仍然可以在我们的本地机器上运行。不幸的是,由于这是 API 授权,因此不容易重现。 en
我是一名优秀的程序员,十分优秀!