分配内存时出错

时间:2014-09-09 03:32:30

标签: c pointers malloc

我在运行代码时一直收到此错误

  

ping:malloc.c:2372:sysmalloc:断言`(old_top ==(((mbinptr)(((char *)&((av) - > bins [((1) - 1)* 2 ])) - __builtin_offsetof(struct malloc_chunk,fd))))&& old_size == 0)|| ((unsigned long)(old_size)> =(unsigned long)(((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t))) - 1))&〜((2 * (sizeof(size_t))) - 1)))&&((old_top) - > size& 0x1)&&((unsigned long)old_end& pagemask)== 0)'失败。

我的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ucontext.h>
#include "mythread.h"
#define STACK 1024*64

ucontext_t init_thr, new_thr;
static int thread_id = 1; 
struct _MyThreadStruct
{
    ucontext_t ctxt;
    int thr_id;
    int states; // 1-Ready, 2-Running, 3-Blocked, 4-Blocked by all, 5-Exit
    struct _MyThreadStruct *parent;
    //struct threadList *children;  
};

typedef struct _MyThreadStruct * _MyThread;
_MyThread currentthread;

struct threadList
{
    _MyThread thr;
    struct threadList *link;
};

struct threadList *front,*rear,*temp,*front1;

_MyThread frontelement();
void enq(_MyThread thr);
void deq();
void empty();
void create();
void queuesize();

int qSize = 0;

MyThread MyThreadCreate(void(*start_funct)(void *), void *args)
{
    getcontext(&new_thr);
    new_thr.uc_link = &currentthread->ctxt;
    new_thr.uc_stack.ss_sp = malloc(STACK);
    new_thr.uc_stack.ss_size = STACK;
    new_thr.uc_stack.ss_flags = 0;
    makecontext(&new_thr, (void (*)(void)) start_funct,1,args);
    _MyThread newthread = (_MyThread )malloc(sizeof(_MyThread));
    newthread->ctxt = new_thr;
    newthread->thr_id = thread_id++;
    newthread->states = 1;
    newthread->parent = currentthread;
    //newthread->children = (struct threadList)malloc(sizeof(struct threadList));
    enq(newthread);
    return (MyThread)newthread;
}
void MyThreadYield()
{
    enq(currentthread);
    currentthread = frontelement();
    deq();
    queuesize();
    setcontext(&currentthread->ctxt);
}
void MyThreadExit()
{
    printf("in Exit");  
}
/* Create an empty queue */
void create()
{
    front = rear = NULL;
}

/* Returns queue size */
void queuesize()
{
    printf("\nQueue size : %d", qSize);
}

/* Enqueing the queue */
void enq(_MyThread thr)
{
    if (rear == NULL)
    {
        rear = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = NULL;
        rear->thr = thr;
        front = rear;
    }
    else
    {
        temp = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = temp;
        temp->thr = thr;
        temp->link = NULL;
        rear = temp;
    }
    qSize++;
}

/* Dequeing the queue */
void deq()
{
    front1 = front; 
    if (front1 == NULL)
    {
        printf("\nNo threads on queue");
        return;
    }
    else
    if (front1->link != NULL)
    {
        front1 = front1->link;
        printf("\nDequed");
        free(front);
        front = front1;
    }
    else
    {
        printf("\nDequed");
        free(front);
        front = NULL;
        rear = NULL;
    }
    qSize--;
}

/* Returns the front element of queue */
_MyThread frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->thr);
    else
        printf("\nQueue empty");
}

/* Display if queue is empty or not */
void empty()
{
    if ((front == NULL) && (rear == NULL))
        printf("\nQueue empty");
    else
        printf("\nQueue not empty");
}

void MyThreadInit (void(*start_funct)(void *), void *args)
{
    getcontext(&init_thr);
    init_thr.uc_link = 0;
    init_thr.uc_stack.ss_sp = malloc(STACK);
    init_thr.uc_stack.ss_size = STACK;
    init_thr.uc_stack.ss_flags = 0;
    makecontext(&init_thr, (void (*)(void)) start_funct,1,args);
    printf("\n00000");
    _MyThread initthread = (_MyThread)malloc(sizeof(_MyThread));
    printf("\n11111");
    initthread->ctxt = init_thr;
    printf("\n22222");
    initthread->thr_id = thread_id;
    initthread->states = 2;
    initthread->parent = NULL;
    //initthread->children = (struct threadList)malloc(sizeof(struct threadList));  
    create();
    currentthread = initthread;
    setcontext(&initthread->ctxt);
}

我从一段单独的代码中调用了MyThreadInit。

失败了
_MyThread initthread = (_MyThread)malloc(sizeof(_MyThread));

有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:1)

我在malloc中使用typedef _MyThread时遇到错误。我使用struct _MyThreadStruct代替它,它工作正常。需要查看typedef的事情。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ucontext.h>
#include "mythread.h"
#define STACK 1024*64

ucontext_t init_thr, new_thr;
static int thread_id = 1; 
struct _MyThread
{
    ucontext_t ctxt;
    int thr_id;
    int states; // 1-Ready, 2-Running, 3-Blocked, 4-Blocked by all, 5-Exit
    struct _MyThread *parent;
    //struct threadList *children;  
};

struct _MyThread * currentthread;

struct threadList
{
    struct _MyThread *thr;
    struct threadList *link;
};

struct threadList *front,*rear,*temp,*front1;

struct _MyThread * frontelement();
void enq(struct _MyThread * thr);
void deq();
void empty();
void create();
void queuesize();

int qSize = 0;

MyThread MyThreadCreate(void(*start_funct)(void *), void *args)
{
    getcontext(&new_thr);
    new_thr.uc_link = &currentthread->ctxt;
    new_thr.uc_stack.ss_sp = malloc(STACK);
    new_thr.uc_stack.ss_size = STACK;
    new_thr.uc_stack.ss_flags = 0;
    makecontext(&new_thr, (void (*)(void)) start_funct,1,args);
    struct _MyThread * newthread = (struct _MyThread *)malloc(sizeof(struct _MyThread));
    newthread->ctxt = new_thr;
    newthread->thr_id = thread_id++;
    newthread->states = 1;
    newthread->parent = currentthread;
    //newthread->children = (struct threadList)malloc(sizeof(struct threadList));
    enq(newthread);
    return (MyThread)newthread;
}
void MyThreadYield()
{
    enq(currentthread);
    currentthread = frontelement();
    deq();
    queuesize();
    setcontext(&currentthread->ctxt);
}
void MyThreadExit()
{
    printf("in Exit");  
}
/* Create an empty queue */
void create()
{
    front = rear = NULL;
}

/* Returns queue size */
void queuesize()
{
    printf("\nQueue size : %d", qSize);
}

/* Enqueing the queue */
void enq(struct _MyThread * thr)
{
    if (rear == NULL)
    {
        rear = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = NULL;
        rear->thr = thr;
        front = rear;
    }
    else
    {
        temp = (struct threadList *)malloc(1*sizeof(struct threadList));
        rear->link = temp;
        temp->thr = thr;
        temp->link = NULL;
        rear = temp;
    }
    qSize++;
}

/* Dequeing the queue */
void deq()
{
    front1 = front; 
    if (front1 == NULL)
    {
        printf("\nNo threads on queue");
        return;
    }
    else
    if (front1->link != NULL)
    {
        front1 = front1->link;
        printf("\nDequed");
        free(front);
        front = front1;
    }
    else
    {
        printf("\nDequed");
        free(front);
        front = NULL;
        rear = NULL;
    }
    qSize--;
}

/* Returns the front element of queue */
struct _MyThread * frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->thr);
    else
        printf("\nQueue empty");
}

/* Display if queue is empty or not */
void empty()
{
    if ((front == NULL) && (rear == NULL))
        printf("\nQueue empty");
    else
        printf("\nQueue not empty");
}

void MyThreadInit (void(*start_funct)(void *), void *args)
{
    getcontext(&init_thr);
    init_thr.uc_link = 0;
    init_thr.uc_stack.ss_sp = malloc(STACK);
    init_thr.uc_stack.ss_size = STACK;
    init_thr.uc_stack.ss_flags = 0;
    makecontext(&init_thr, (void (*)(void)) start_funct,1,args);
    struct _MyThread * initthread = (struct _MyThread *)malloc(sizeof(struct _MyThread));
    initthread->ctxt = init_thr;
    initthread->thr_id = thread_id;
    initthread->states = 2;
    initthread->parent = NULL;
    //initthread->children = (struct threadList)malloc(sizeof(struct threadList));  
    create();
    currentthread = initthread;
    setcontext(&initthread->ctxt);
}