- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 C 语言研究 Segdwick 的算法,并尝试动态链表数组。我在 main
的 return 0
处遇到段错误。我的重点是正确加载和打印链接列表,完成后我没有释放列表数组。
所以我添加了 freeList
函数,现在得到 pointer being freed was not allocated
。显然,我缺少指针/取消引用的细微差别。有什么建议吗?
lldb
运行:
a.out(3057,0x7fffb470c380) malloc: *** error for object 0x100300470: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
44Process 3057 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00007fff7c22cb6e libsystem_kernel.dylib`__pthread_kill + 10
libsystem_kernel.dylib`__pthread_kill:
-> 0x7fff7c22cb6e <+10>: jae 0x7fff7c22cb78 ; <+20>
0x7fff7c22cb70 <+12>: movq %rax, %rdi
0x7fff7c22cb73 <+15>: jmp 0x7fff7c223b00 ; cerror_nocancel
0x7fff7c22cb78 <+20>: retq
Target 0: (a.out) stopped.
运行a.out
:
a.out(3061,0x7fffb470c380) malloc: *** error for object 0x7fbac1c02970: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
44Abort trap: 6
代码:
#include <stdlib.h>
#include <stdio.h>
#define maxV 13
struct node {
int v; // vertex number
struct node *next;
};
// load array of linked lists
static void load_adjlist_array(struct node ***adj){
// load adjacency array
int j,x,y,V,E;
struct node *t;
int idx[maxV]; // vertex location on adjacency matrix
// ALL edges, single way (combinations)
char v1[maxV] = {'A','A','A','L','J','J','J','E','F','H','F','A','G'};
char v2[maxV] = {'G','B','C','M','M','L','K','D','D','I','E','F','E'};
V = maxV; // number of vertices
E = maxV; // number of edges
// setup vertex positions in linked list
for (j=0;j<V;j++) idx[j] = j;
// setup head for each vertex (vertex connected to NULL by default)
for (j=0;j<V;j++){
t = (struct node *) malloc(sizeof *t); // pointer to allocated node memory
if(t!= NULL){
t->v = j;
t->next = NULL;
adj[j] = &t;
} else return;
}
// for each edge ('AB'), update the relevant linked list: add node to head of that vertex's list
for (j=0;j<E;j++) {
// edge xy: vertex numbers (also positions in idx)
x = idx[v1[j]-'A'];
y = idx[v2[j]-'A'];
// load x data into t, then add t to y's adj list
//printf("handling %c\n",v1[j]);
t = (struct node *) malloc(sizeof *t);
if(t!=NULL){
t->v = x;
t->next = *adj[y];
*adj[y] = t;
} else return;
// add y to x's adj list
//printf("handling %c\n",v2[j]);
t = (struct node *) malloc(sizeof *t);
if(t!=NULL){
t->v = y;
t->next = *adj[x];
*adj[x] = t;
} else return;
}
printf("load_adjlist_array completed\n");
}
static void freeList(struct node* head)
{
struct node* tmp;
printf("\n");
while (head != NULL)
{
tmp = head;
head = head->next;
printf("%d",head->v);
free(tmp);
}
}
int main(){
int j;
struct node **adj; // pointers to adjacency list
adj = malloc(maxV * sizeof(struct node *)); // allocates pointers to linked lists
if (adj==NULL) return -1;
load_adjlist_array(&adj); // allocates memory to linked lists in adj
for (j=0;j<maxV;j++){
freeList(adj[j]);
}
free(adj);
return 0 ;
}
最佳答案
切勿使用三重星指针(例如 struct node ***adj
)。它们几乎总是会产生不好的/不可读的结果。有更清洁的替代方法。在这里,双星可能就是您想要的。
如 MFisherKDX 所述,执行 adj[j] = &t;
是将基于堆栈的指针变量 t
的地址保存到数组中,而不是 t
指向。要解决此问题,函数参数必须使用struct node **adj
这是您的代码的一个版本。您的原始代码 [that needs to be changed] 包装如下:
#if 0
// original code
#else
// fixed code
#endif
这是您的代码已修复(旁注:不要强制转换 malloc
的返回值)。另外,请注意原始 freeList
中的 printf
会取消引用空指针,因此我修复了 [没有 #if 0
配对]:
#include <stdlib.h>
#include <stdio.h>
#define maxV 13
struct node {
int v; // vertex number
struct node *next;
};
// load array of linked lists
static void
#if 0
load_adjlist_array(struct node ***adj)
#else
load_adjlist_array(struct node **adj)
#endif
{
// load adjacency array
int j,
x,
y,
V,
E;
struct node *t;
int idx[maxV]; // vertex location on adjacency matrix
// ALL edges, single way (combinations)
char v1[maxV] = { 'A', 'A', 'A', 'L', 'J', 'J', 'J', 'E', 'F', 'H', 'F', 'A', 'G' };
char v2[maxV] = { 'G', 'B', 'C', 'M', 'M', 'L', 'K', 'D', 'D', 'I', 'E', 'F', 'E' };
V = maxV; // number of vertices
E = maxV; // number of edges
// setup vertex positions in linked list
for (j = 0; j < V; j++)
idx[j] = j;
// setup head for each vertex (vertex connected to NULL by default)
for (j = 0; j < V; j++) {
t = (struct node *) malloc(sizeof *t); // pointer to allocated node memory
if (t != NULL) {
t->v = j;
t->next = NULL;
#if 0
adj[j] = &t;
#else
adj[j] = t;
#endif
}
else
return;
}
// for each edge ('AB'), update the relevant linked list: add node to head of that vertex's list
for (j = 0; j < E; j++) {
// edge xy: vertex numbers (also positions in idx)
x = idx[v1[j] - 'A'];
y = idx[v2[j] - 'A'];
// load x data into t, then add t to y's adj list
// printf("handling %c\n",v1[j]);
t = (struct node *) malloc(sizeof *t);
if (t != NULL) {
t->v = x;
#if 0
t->next = *adj[y];
#else
t->next = adj[y];
#endif
#if 0
*adj[y] = t;
#else
adj[y] = t;
#endif
}
else
return;
// add y to x's adj list
// printf("handling %c\n",v2[j]);
t = (struct node *) malloc(sizeof *t);
if (t != NULL) {
t->v = y;
#if 0
t->next = *adj[x];
#else
t->next = adj[x];
#endif
#if 0
*adj[x] = t;
#else
adj[x] = t;
#endif
}
else
return;
}
printf("load_adjlist_array completed\n");
}
static void
freeList(struct node *head)
{
struct node *tmp;
printf("\n");
while (head != NULL) {
tmp = head;
head = tmp->next;
printf("%d", tmp->v);
free(tmp);
}
}
int
main()
{
int j;
#if 0
struct node **adj; // pointers to adjacency list
#else
struct node **adj; // pointers to adjacency list
#endif
adj = malloc(maxV * sizeof(struct node *)); // allocates pointers to linked lists
if (adj == NULL)
return -1;
#if 0
load_adjlist_array(&adj); // allocates memory to linked lists in adj
#else
load_adjlist_array(adj); // allocates memory to linked lists in adj
#endif
for (j = 0; j < maxV; j++) {
freeList(adj[j]);
}
free(adj);
return 0;
}
这是一个清理后的版本:
#include <stdlib.h>
#include <stdio.h>
#define maxV 13
struct node {
int v; // vertex number
struct node *next;
};
// load array of linked lists
static void
load_adjlist_array(struct node **adj)
{
// load adjacency array
int j,
x,
y,
V,
E;
struct node *t;
int idx[maxV]; // vertex location on adjacency matrix
// ALL edges, single way (combinations)
char v1[maxV] = { 'A', 'A', 'A', 'L', 'J', 'J', 'J', 'E', 'F', 'H', 'F', 'A', 'G' };
char v2[maxV] = { 'G', 'B', 'C', 'M', 'M', 'L', 'K', 'D', 'D', 'I', 'E', 'F', 'E' };
V = maxV; // number of vertices
E = maxV; // number of edges
// setup vertex positions in linked list
for (j = 0; j < V; j++)
idx[j] = j;
// setup head for each vertex (vertex connected to NULL by default)
for (j = 0; j < V; j++) {
t = malloc(sizeof *t); // pointer to allocated node memory
if (t != NULL) {
t->v = j;
t->next = NULL;
adj[j] = t;
}
else
return;
}
// for each edge ('AB'), update the relevant linked list: add node to head of that vertex's list
for (j = 0; j < E; j++) {
// edge xy: vertex numbers (also positions in idx)
x = idx[v1[j] - 'A'];
y = idx[v2[j] - 'A'];
// load x data into t, then add t to y's adj list
// printf("handling %c\n",v1[j]);
t = malloc(sizeof *t);
if (t != NULL) {
t->v = x;
t->next = adj[y];
adj[y] = t;
}
else
return;
// add y to x's adj list
// printf("handling %c\n",v2[j]);
t = malloc(sizeof *t);
if (t != NULL) {
t->v = y;
t->next = adj[x];
adj[x] = t;
}
else
return;
}
printf("load_adjlist_array completed\n");
}
static void
freeList(struct node *head)
{
struct node *tmp;
printf("\n");
while (head != NULL) {
tmp = head;
head = tmp->next;
printf("%d", tmp->v);
free(tmp);
}
}
int
main()
{
int j;
struct node **adj; // pointers to adjacency list
// allocates pointers to linked lists
adj = malloc(maxV * sizeof(struct node *));
if (adj == NULL)
return -1;
load_adjlist_array(adj); // allocates memory to linked lists in adj
for (j = 0; j < maxV; j++) {
freeList(adj[j]);
}
free(adj);
return 0;
}
这里是固定程序的输出:
load_adjlist_array completed
52160
01
02
543
6534
0435
406
87
78
1011129
910
91211
91112
关于c - 链表 : Pointer being freed was not allocated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53549859/
我正在使用套接字并将一些数据写入服务器。第一次连接到服务器时一切正常。但是当它第二次写入,有时是第三次写入时,它会因错误而崩溃: "malloc: *** error for object 0x7c1
给定代码: class Sample { public: int *ptr; Sample(int i) { ptr = new int(i);
#include #include #include #include #include using namespace std; class CFile { public: CFi
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。为了帮助澄清这个问题以便可以重新打开它,visit the help center
我正在尝试在线找到的四叉树实现,但作为以下(精简的)_node_release 函数的一部分,间歇性地收到“正在释放的指针未分配”错误: static void _node_release(node*
我正在尝试在线找到的四叉树实现,但作为以下(精简的)_node_release 函数的一部分,间歇性地收到“正在释放的指针未分配”错误: static void _node_release(node*
我正在尝试运行一个删除链表第 n 个元素的函数(使用从零开始的索引)。即使我不必 malloc 任何东西,我也会收到此错误:“ev(10676,0x7fff73f9d300) malloc: * er
我正在从标准输入读取内容。由于我不知道要读的内容的长度,所以我必须使用 malloc。 我得到一个被释放的指针未分配 有时,它发生在 free(final) 之前,有时发生在 free(tmp) 之前
我得到一个错误 malloc: *** error for object 0x146f9404: incorrect checksum for freed object - object was
我正在使用 C++ 进行线程处理并进行了一些测试并遇到了这个错误。 这是我的代码: #include #include #include #include #include using na
我有以下功能: void stringcopy(char * to, char const * const from) { int size = 1; while (from[size
每次我想将元素添加到存储类中的 std::map 时,我都会收到错误消息“未分配正在释放的指针”。我在构造函数和析构函数中添加了一些“couts”来调试,输出是: 新的德尔删除 所以看起来析构函数被调
我定义了一个包含字节数组及其长度的结构。析构函数应该只删除字节数组,如果它是由结构的构造函数动态实例化的话。但有时,delete array; 指令失败并出现错误 pointer being free
我正在构建一个 AVL 树。我有一种方法可以删除树中的项目,但出现错误。 这是我得到的运行时错误: malloc: *** error for object 0x100100120: pointer
这是我的功能: void Tetris::place_square(int* coords,char type){ if (coords[1]>heights[coords[0]]){
我创建了一个双链表类,并试图将它与我创建的 Vector 类一起使用,以便制作一个链表 vector ,但是在程序结束时,我似乎遇到了一个错误malloc:对象 0x100100be0 的 *** 错
我想我的 C 现在有点生疏了,因为我不太明白这里的问题。我很确定它位于 parse_historical_data() 中。如果我将其注释掉并运行 allocate_historical_data()
我正在使用 C 语言研究 Segdwick 的算法,并尝试动态链表数组。我在 main 的 return 0 处遇到段错误。我的重点是正确加载和打印链接列表,完成后我没有释放列表数组。 所以我添加了
我正在努力寻找无法释放内存块的原因。指针一定有问题。结构的内存块在函数中创建,使用的指针存储在数组中。稍后从数组中获取指针以用于释放内存。 我已经弄清楚它是免费的了。我在它旁边放了“//这个”。 #i
我正在尝试重载赋值运算符以执行多边形对象的深拷贝,程序编译但我在接近尾声时收到错误,我想清除。以下是相关代码,如果您认为我需要添加更多内容,请发表评论。假设适当的 #include的那 class P
我是一名优秀的程序员,十分优秀!