C语言:创建链表时,内存申请出错

问题描述:

创建链表中,在申请结点的内存时出现了问题
#include <stdio.h>
#include <string.h>

#define MAXSIZE 20//字符串的最大长度
#define T 3//阈值

typedef struct data_splited {//数据包的数据域
    char* s;
    int length;
    char* p;
}Data_splited;
typedef struct list_splited {//将字符串切分后的数据包
    Data_splited data;
    struct list_splited* next;
}List_Splited;
typedef struct//创建函数返回的类型,其返回的字串最多为20个
{
    char S[10][MAXSIZE];
}Result_split;
![img](https://img-mid.****img.cn/release/static/image/mid/ask/943192212936180.png "#left")


List_Splited* Split(char*);



int main()
{
    List_Splited* result = Split("helloworld");

    return 0;
}


List_Splited* Split(char* str)
{
    if (str == NULL)
    {
        printf("您输入的字符为空!");
        return;
    }
    else
    {
        Result_split result = split(str);
        int length = strlen(str);
        int n = length / T;

        //创建头、尾结点
        List_Splited* temp;
        int i = 1;
        List_Splited* head = NULL;
        List_Splited* tail = NULL;
        temp = (List_Splited*)malloc(sizeof(List_Splited));
        head = temp;
        temp->data.s = NULL;
        //数据输入
        strcpy_s(temp->data.s, 10, result.S[0]);
        temp->data.length = length;
        temp->data.p = str;
        //
        head->next = NULL;
        tail = temp;
        temp = NULL;
        while (i < n)
        {
            temp = (List_Splited*)malloc(sizeof(List_Splited));
            tail->next = temp;
            //数据输入
            strcpy_s(temp->data.s, 10, result.S[i]);
            temp->data.length = length;
            temp->data.p = str;
            //
            temp->next = NULL;
            tail = temp;
            temp = NULL;
            i++;
        }
        return head;
    }
}
申请内存前

img

申请内存后

img

如果是内存分配的错,加个头文件#include

增加这两行定义
#include <crtdbg.h>
#include <corecrt_malloc.h>