我最近开始学习C并正在编写一些应用程序,我遇到了这个错误,我从未见过,显然是在Mallocing
时得到哪个错误 malloc.c:2394: Assertion (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0) failed.
Aborted (core dumped)
这是代码段
typedef struct {
Node *root;
pthread_id id;
pid_t tid;
}Thread;
typedef struct {
Thread **thread;
int cap, len;
}arr;
static __thread Thread *self = NULL;
pthread_mutex_t lock;
static arr p;
static void
foo (void)
{
pthread_mutex_lock(&lock);
Thread **newthread;
if (p.len == p.cap){
if (p.cap != 0){
newthread = (Thread **) realloc(p.thread, (1+p.cap)*sizeof(Thread *));
}
else {
newthread = (Thread **)malloc(sizeof(Thread *));
}
if (!newthread){
printf("appendThreadpointer: Error while allocating sizen");
exit(1);
}
p.thread=newthread;
++p.cap;
}
++p.len;
Thread *t = malloc(sizeof(Thread *));
t->tid = syscall(SYS_gettid);
t->root = newNode();
t->id = pthread_self();
p.thread[p.len-1] = t;
self = t;
pthread_mutex_unlock(&lock);
}
任何人都可以看到发生这种情况的原因吗?最奇怪的是,如果我从 Thread
struct中删除 tid
字段,那么它可以正常工作,只有当我包括 tid
字段时,它才会给我这个错误。
很困惑,感谢您的任何帮助,谢谢。
Thread *t = malloc(sizeof(Thread *));
可能应该更改为
Thread *t = malloc(sizeof(Thread));