链接列表 - 无法创建更长的链接列表

时间:2014-05-16 16:05:29

标签: c pointers linked-list

heres funciton,这是推箱子游戏。

LEVEL *load_levels(char *path) {
    FILE *levels;

    levels = fopen(path,"rw");
    if (levels == NULL) 
        printf("No such file\n");

    fseek(levels,0,SEEK_END);
    long lSize = ftell(levels);
    rewind(levels);

    char *maps = (char*)malloc(sizeof(char)*lSize);
    fread(maps, 1, lSize, levels);

    char* buffer = (char*)malloc(1025); 

    LEVEL *level = NULL,*root,*prev;
    root = NULL,prev = NULL;

    while (1) {            
      level = (LEVEL*)malloc(sizeof(LEVEL));
      while (*maps != '\n') {
            addChar(buffer,*maps);
            maps++;
            if (*maps == '\0')
                break;
        }

        level = parse_level(buffer);
        level->next = NULL;           
        if (root == NULL)
            root = level;
        else 
            prev->next = level;

        prev = level;
        strcpy(buffer,"");
        if (*maps == '\0')
           break;

        maps++;
   }

   level = root;
   return level;  
}

关键是,从FILE看起来像这样:http://textuploader.com/9slp,这个函数无法获取此FILE中的所有地图,它会崩溃:

malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr)(((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof(struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) -1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

但是,如果我删除至少一半的文件,则会发生e错误。任何想法?

Level是一个包含的结构:

char *name, 
char *description, 
char* password, 
char* map, 
level* next

...和parse_level解析(例如):

芝加哥;艾迪;故事从这里开始;

-----#####-----------|-----#@$.#-----------|-----#####----------- 

namepassworddestinationmap

0 个答案:

没有答案