I am new to C and I am using memset
.
From what I read memset
replaces part of a section of memory with a char.
When I try to do that my program shuts down and using breakpoints it stops after the line with memset
. Not really sure why.
void init_TCB (struct TCB_t *tcb, void *function, void *stackP, int stack_size)
{
memset(tcb, '\0', sizeof(struct TCB_t));
getcontext(&tcb->context);
tcb->context.uc_stack.ss_sp = stackP;
tcb->context.uc_stack.ss_size = (size_t)stack_size;
makecontext(&tcb->context, function, 0);
}
the sizeof(struct TCB_t)
is 957, tcb
is the memory location for the struct
, and '\0'
is a char
.
Here is the struct `
struct TCB_t
{
struct TCB_t * next;
struct TCB_t * previous;
ucontext_t context;
};
and here is where I initialize the struct
void start_thread(void (*function)(void))
{
struct stack * stackP = (struct stack*)malloc(sizeof(struct stack));
struct TCB_t * tcb = (struct TCB_t *)(sizeof(struct TCB_t));
init_TCB (tcb, function, stackP, 8192);
ptr = create_list();
add_to_list( ptr);
}
This line is wrong:
struct TCB_t * tcb = (struct TCB_t *)(sizeof(struct TCB_t));
You're pointing tcb to the size of the structure.
Instead you could allocate space for the structure with malloc()
:
struct TCB_t * tcb = malloc(sizeof(struct TCB_t));