Categories
Development

Function will only properly work if I’ve printed the values of the attributes

I am trying to create a library for college that handles musical partitions from XML files.
I don’t want to get into much detail, but I have a function that creates the partition score and another that adds individual notes score_add_note.

The prototypes are as follows :

score_t* score(size_t tempo, size_t duration, size_t sig, size_t nbInstr)
void score_add_note(score_t* sc, size_t ins, size_t bar, pitch_t p, size_t start, size_t duration)

I have also transformed the XML file into a structure that I have made, it is called e in this case.

In one occasion, I used these functions like this :

score_t* sc = score(atoi(e->at_head->value), atoi(e->at_head->next->value), atoi(e->at_head->next->next->value), atoi(e->at_head->next->next->next->value));
score_add_note(sc, 0, 0, 60, 0, 6);

The attributes are part of the XML structure.

This doesn’t work, the score_add_note function never stops executing.

However, if I print the values from the attributes to the score function beforehand, it works correctly:

printf("%d\n", atoi(e->at_head->value));
printf("%d\n", atoi(e->at_head->next->value));
printf("%d\n", atoi(e->at_head->next->next->value));
printf("%d\n", atoi(e->at_head->next->next->next->value));

score_t* sc = score(atoi(e->at_head->value), atoi(e->at_head->next->value), atoi(e->at_head->next->next->value), atoi(e->at_head->next->next->next->value));
score_add_note(sc, 0, 0, 60, 0, 6);

Am I making some mistake or is this a bug in C. If so, is there any way around it?
Btw, I am using gcc as my compiler.

EDIT:
As asked, here is the score_add_note function

void score_add_note(score_t* sc, size_t ins, size_t bar, pitch_t p, size_t start, size_t duration){
    if(sc->system[ins].tab[bar] == NULL){
        sc->system[ins].tab[bar] = (bar_t*) malloc (sizeof(bar_t));
        sc->system[ins].tab[bar]->start = start;
        sc->system[ins].tab[bar]->duration = duration;
        sc->system[ins].tab[bar]->pitch = p;
    }else{
        note_t* note = sc->system[ins].tab[bar];
        while(1){
            if(note->next == NULL){
                note->next = (note_t*) malloc (sizeof(note_t));
                note = note->next;
                break;
            } 
            note = note->next;
        }
        note->start = start;
        note->duration = duration;
        note->pitch = p;
    } 
}

There is a chance that the function is stuck in an infinite loop, but the arguments are the same in both cases.

This is the score function:

score_t* score(size_t tempo, size_t duration, size_t sig, size_t nbInstr){
    score_t* sc = (score_t*) malloc (sizeof(score_t));
    sc->tempo = tempo / 60;
    sc->duration = duration;
    sc-> time_sig = sig;
    sc->sys_size = nbInstr;
    sc->system = (staff_t*) malloc (nbInstr * sizeof(staff_t));
    for(int i = 0; i < nbInstr; i++){
        sc->system[i].tab = (bar_t**) malloc (duration * sizeof(bar_t*));
    }
    return sc;
}

These are the types I have created:

typedef struct note_s{
    size_t start;
    size_t duration;
    pitch_t pitch;
    struct note_s* next;
}note_t;


typedef note_t bar_t;

typedef struct staff_s{
    bar_t** tab;
}staff_t;


struct score_s{
    size_t tempo;
    size_t duration;
    size_t time_sig;
    size_t sys_size;
    staff_t* system;
};

Leave a Reply

Your email address will not be published. Required fields are marked *