I'm having some problems with this function:
link_t* createFrame(char name[], int duration, char path[]){
link_t* newFrame = (link_t*)malloc(sizeof(link_t));
newFrame->frame= (frame_t*)malloc(sizeof(frame_t));
newFrame->frame->duration = duration;
newFrame->frame->name = (char*)malloc((MAX_NAME_SIZE + 1) * sizeof(char));
newFrame->frame->name[MAX_NAME_SIZE] = 0; //Manually add null termination
strncpy(newFrame->frame->name, name, MAX_NAME_SIZE);
newFrame->frame->path = (char*)malloc((MAX_NAME_SIZE + 1) * sizeof(char));
newFrame->frame->path[MAX_PATH_SIZE] = 0;
strncpy(newFrame->frame->path, path, MAX_PATH_SIZE);
newFrame->next = NULL;
return newFrame;
}
And here's the linked list and the struct:
#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)
struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
};
typedef struct Frame frame_t;
struct Link
{
frame_t *frame;
struct Link *next;
};
typedef struct Link link_t;
The problem is when I use the function in a loop, the first time it works but the next time it doesn't. I tried to debug the code step by step and I found out that the problem is in the second run of the loop, in this line of code:
link_t* newFrame = (link_t*)malloc(sizeof(link_t));
Maybe I need to free something in the loop?