分配内存时c malloc断言失败

分配内存时c malloc断言失败,c,malloc,C,Malloc,因此,我的代码中有print语句,我在dumpTB中malloce,它似乎抛出了一个sysmalloc断言失败并中止了我的程序。我一辈子都搞不懂为什么…我打印出了数字totalsLength和它的36,这是textBuffer中字符串的长度,我验证了它是正确的。谁能告诉我是什么问题吗 编辑:请求新代码 typedef struct textNode{ //basically contains a line + link char* line; int sL

因此,我的代码中有print语句,我在dumpTB中malloce,它似乎抛出了一个sysmalloc断言失败并中止了我的程序。我一辈子都搞不懂为什么…我打印出了数字totalsLength和它的36,这是textBuffer中字符串的长度,我验证了它是正确的。谁能告诉我是什么问题吗

编辑:请求新代码

    typedef struct textNode{ //basically contains a line + link
        char* line;
        int sLength; //length of string
        struct textNode* next; 
    } tNode; 

    struct textbuffer{
        int size; //number of lines in the text buffer
        int totalsLength;
        tNode* head; 
        tNode* tail;
    };

char *dumpTB (TB tb){

    int stringLength = tb->totalsLength; //sLength is 
    char* text = malloc(sizeof(char) * stringLength+1);

    int i = 0;
    int x = 0; //string index

    tNode* curr = tb->head;

    while(curr != NULL){

        while(curr->line[x] != '\n'){
            printf("%d", i);
            text[i] = curr->line[x];
            printf("%c\n", text[i]);

            i++;
            x++;
        }
        printf("%d\n", i);
        text[i] = '\n';
        printf("%c", text[i]);
        i++;

        x = 0; 
        curr = curr->next; 
    }

    text[tb->totalsLength] = '\0';

    return text;
}
编辑:添加主

static tNode* newTN(char* string, int sLength, tNode* next){
    tNode* t = malloc(sizeof(struct textNode));
    t->line = malloc(sizeof(char)*sLength);

    if(string != NULL){
        strcpy(t->line, string);
    } 
    t->next = next; 
    return t;
}

TB newTB (char text[]){

    assert(text != NULL); 

    int i = 0; 
    char c;

    TB tBuffer = malloc(sizeof(struct textbuffer)); 
    tBuffer->size = 0; 

    //tNode* currLine = malloc(sizeof (struct textNode*));
    tNode* currLine = newTN(NULL, 1, NULL);
    tNode* currtNode = NULL; 

    //currLine->line = malloc(sizeof(char));

    while(1){

        c = text[i];

        if(c == '\0'){
            break;
        } else{
            currLine->line = realloc(currLine->line, currLine->sLength+1);
        }

        currLine->line[currLine->sLength] = c;

        if(c == '\n'){ // create new textNode to contain string

            if(tBuffer->size == 0){

                tBuffer->head = newTN(currLine->line, currLine->sLength, NULL);     
                currtNode = tBuffer->head; 

            } else{

                currtNode->next = newTN(currLine->line, currLine->sLength, NULL);
                tBuffer->tail = currtNode->next;
                currtNode = currtNode->next; 
            }

            tBuffer->totalsLength += currLine->sLength+1; //account for \n
            currLine->line = realloc(currLine->line, 0);         
            currLine->sLength = 0;
            tBuffer->size++;
            i++;
            continue;
        }

        currLine->sLength++;
        i++;
    }
    free(currLine->line);
    free(currLine);

    //printBuffer(tBuffer);
    return tBuffer;
}
#包括
#包括
#包括
#包括“textbuffer.h”
int main(int argc,char*argv[]){
TB tb1=newTB(“HI\nHIYO\nHELLO\nwut\nyolo\nugnad\n”);
//棉签(tb1,0,5);
字符*文本=转储TB(tb1);
int i=0;
while(text[i]!=“d”){
printf(“%c”,文本[i]);
i++;
}
printf(“\n”);
释放TB(tb1);
返回退出成功;
}
好的,
text
具有
stringLength+1
可访问字节

char* text = malloc(sizeof(char) * stringLength+1);
当且仅当
i
小于或等于
stringLength
时,这才是合法的。因此,让我们先添加一些代码:

    text[i] = '\n';
现在让我们编译并运行它:

30
出界访问:i=30 stringLength=29
31
出界访问:i=31 stringLength=29
32
出界访问:i=32 stringLength=29
33
出界访问:i=33 stringLength=29


休斯顿,我们有一个问题。

你能给我们足够的代码来复制错误吗?这应该足够了,只要确保向数组中传递一个文本,如下所示:“hi\nhello\n计算机\n”,其中每一位都以“\n”结尾,因为这表示我tb中的一行。@DavidSchwartz这段代码足够复制它吗?没有
main()
函数。每当您从
malloc
内部收到任何类型的错误消息时,您的第一个假设应该是内存损坏,您应该首先尝试在
valgrind
下运行程序。
    text[i] = '\n';
    if (i > stringLength)
        printf("ACCESS OUT OF BOUNDS: i=%d stringLength=%d\n", i, stringLength);