我最近开始学习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 size\n");
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);
}
任何人都可以看到为什么会发生这种情况的原因吗?最奇怪的是,如果我从tid
结构中删除Thread
字段,那么它工作正常,只有在我包含tid
字段时才会出现此错误。
很困惑,对此有任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
Thread *t = malloc(sizeof(Thread *));
可能应该改为
Thread *t = malloc(sizeof(Thread));