Pergunta

I've been trying to debug this for hours.


I have a struct for a scheduler.

typedef struct rr_scheduler {
    unsigned int time_q;
    unsigned int avg_wait;
    unsigned int avg_turnaround;
    unsigned int processes_served;
    unsigned int t;
    unsigned int next_dispatch_t;
    Process* cp;
    LinkedList* queue;
    LinkedList* done_list;
} rr_scheduler;

It's being initialised as so:

rr_scheduler* new_rr_scheduler(unsigned int time_q) {
    rr_scheduler* rr = NULL;

    rr = (rr_scheduler*)malloc(sizeof rr);

    rr->time_q = time_q;
    rr->queue = newLinkedList();
    rr->done_list = newLinkedList();
    rr->avg_wait = 0;
    rr->avg_turnaround = 0;
    rr->processes_served = 0;
    rr->t = 0;
    rr->next_dispatch_t = 0;
    rr->cp = NULL;

    return rr;
}

Here's the linkedList initialisation function. (Yes, I know I don't need to set values to null if I'm using calloc)

LinkedList* newLinkedList() {
    LinkedList* newList = (LinkedList*)calloc(1, sizeof (LinkedList));
    newList->head = NULL;
    newList->tail = NULL;
    newList->current = NULL;
    newList->length = 0;
    return newList;
}

After pulling my hair out for ages, I noticed that two unrelated variables changed simultaneously. Turns out, scheduler->cp and scheduler->queue->tail seem to share a memory address.

Picture of same address phenomenon

Here, s is an rr_scheduler pointer.

Any possible reasons for this would be greatly appreciated.

Foi útil?

Solução

sizeof rr

is the size of the pointer. But you need to allocate enough memory for the struct. Use

sizeof *rr

or

sizeof(rr_scheduler)

to do that.

As it stands in your program, you don't allocate enough memory for the struct and so write beyond the end of the block, thus corrupting the heap.

It also seems odd to initialise rr to NULL and then immediately assign to it. I would write that as

rr_scheduler* rr = malloc(sizeof *rr);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top