下面是代码:
由于某种原因,while循环中的calloc在第二次迭代中失败。
看起来堆被破坏了(不确定),但不清楚根本原因。
也请看一下在那里添加的评论。
快速反应。

#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include<stdio.h>
#include <stdlib.h>

struct User_
{
    char* id;
    char* firstName;
    char* lastName;
    int age;
    char gender[2];
    char* userName;
    char* password;
    char* description;
    char hobbies[2];
}typedef User;


void replaceEnterInString(int lengthString, char* string, int  maxChars);




int main()
    {
        char str1[500] = "012345678;danny;cohen;22;M;danny1993;123;1,2,4,8;Nice person";
        char str2[500] = "223325222;or;dan;25;M;ordan10;1234;3,5,6,7;Singer and dancer";


    int j = 0;
    char *token = NULL, arrangingHobbies;
    int lengthStr, tempAge, hobby[4], i;

    while(j<2)
    {

        User* newUser = NULL;

在这里,它第一次通过,但第二次失败。但仅当添加将token映射到newUser的代码时。如果没有映射,就要根据需要一次又一次地管理用户
错误代码:检测到严重错误c0000374-TEST.exe已触发断点。
        newUser = (User*)calloc(1, sizeof(User));
        if (newUser == NULL)
        {
            printf("error");
            exit(1);
        }

        //start map string to user
        if (j == 0)
        {
            token = strtok(str1, ";");
            printf("%s", str1);
        }
        else {
            token = strtok(str2, ";");
            printf("%s", str2);
        }

        //Input ID
        newUser->id = (char*)calloc(10, sizeof(char));
        if (newUser->id == NULL)
        {
            printf("error");
            exit(1);
        }
        strcpy(newUser->id, token);

        //Input first name
        token = strtok(NULL, ";");
        lengthStr = strlen(token);
        newUser->firstName = (char*)calloc((lengthStr + 1), sizeof(char));
        if (newUser->firstName == NULL)
        {
            printf("error");
            exit(1);
        }
        strcpy(newUser->firstName, token);

        //Input last name
        token = strtok(NULL, ",;");
        lengthStr = strlen(token);
        newUser->lastName = (char*)calloc((lengthStr + 1), sizeof(char));
        if (newUser->lastName == NULL)
        {
            printf("error");
            exit(1);
        }
        strcpy(newUser->lastName, token);

        //Input Age
        token = strtok(NULL, ",;");
        tempAge = atoi(token);
        newUser->age = tempAge;

        //Input gender
        token = strtok(NULL, ",;");
        newUser->gender[0] = token[0];


        //Input User Name
        token = strtok(NULL, ",;");
        lengthStr = strlen(token);
        newUser->userName = (char*)calloc((lengthStr), sizeof(char));
        if (newUser->userName == NULL)
        {
            printf("error");
            exit(1);
        }
        strcpy(newUser->userName, token);

        //Input password
        token = strtok(NULL, ",;");
        lengthStr = strlen(token);
        newUser->password = (char*)calloc((lengthStr), sizeof(char));
        if (newUser->password == NULL)
        {
            printf("error");
            exit(1);
        }
        strcpy(newUser->password, token);

        //Input hobbies
        newUser->hobbies[0] = 0;
        for (i = 0; i < 4; ++i)
        {
            token = strtok(NULL, ",;");
            tempAge = atoi(token);
            arrangingHobbies = 1;
            arrangingHobbies <<= (tempAge - 1);
            newUser->hobbies[0] |= arrangingHobbies;
        }

        //Input description
        token = strtok(NULL, ",;");
        newUser->description = (char*)calloc((lengthStr), sizeof(char));
        if (newUser->description == NULL)
        {
            printf("error");
            exit(1);
        }
        replaceEnterInString(strlen(token), token, 300);
        strcpy(newUser->description, token);

        j++;
    }

}

void replaceEnterInString(int lengthString, char* string, int  maxChars)
{
    if (lengthString < maxChars)
    {
        //remove the /n
        string[lengthString - 1] = '\0';
    }
}

最佳答案

也许还有其他问题,但以下代码肯定会导致未定义的行为:

lengthStr = strlen(token);
newUser->userName = (char*)calloc((lengthStr), sizeof(char));
...
strcpy(newUser->userName, token);

在之前的类似语句中,您正确地编写了... = (char*)calloc((lengthStr+1), sizeof(char));
顺便说一句:在C语言中,你通常不需要将malloc的结果进行转换,sizeof(char)的定义总是1的,如果你用随后的0填充内存,就不需要使用calloc将内存设置为strcpy。所以你应该写。。。
lengthStr = strlen(token);
newUser->userName = malloc(lengthStr+1);
...
strcpy(newUser->userName, token);

请仔细查看您的代码以查找类似问题。

关于c - Calloc第二次发行-C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54014126/

10-12 16:04