我最近开始学习C,并写一些应用程序,我得到这个错误,我从来没有见过的,当我mallocingC:malloc的断言失败
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);
}
任何人都可以看到这种情况发生的原因吗?最奇怪的是,如果我从Thread
结构中删除tid
字段,那么它工作正常,它只给我这个错误,当我包含tid
字段。
我很困惑,任何帮助,将不胜感激,谢谢。
'主题* T =的malloc(的sizeof(螺纹*));' - 这看起来腥。你为线程指针分配空间,但是你需要一个线程空间:'Thread * t = malloc(sizeof(Thread));''或者更好,Thread * t = malloc(sizeof(* t));'' 。 (你得到的错误可能与下面的分配't'有关,因此稍后写入越界,你就会破坏动态内存的C库的household数据。) –
ohh man,这样一个愚蠢的错误,是的,你是对的,非常感谢, – Nik391