我在使用此功能时遇到了一些问题:
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;
}
这里是链接列表和结构:
#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;
问题是当我在循环中使用该函数时,它第一次工作,但下次它没有。我试图逐步调试代码,我发现问题出现在循环的第二次运行中,在这行代码中:
link_t* newFrame = (link_t*)malloc(sizeof(link_t));
也许我需要在循环中释放一些东西?