gpt4 book ai didi

c - 分配内存时出错

转载 作者:行者123 更新时间:2023-11-30 15:30:11 32 4
gpt4 key购买 nike

运行代码时我不断收到此错误,

ping: malloc.c:2372: sysmalloc: Assertion `(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)' failed.

我的代码如下:

#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));

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

最佳答案

我在 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);
}

关于c - 分配内存时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25736293/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com