第二次calloc问题-C

时间:2019-01-02 22:41:39

标签: c calloc

bellow是代码:

由于某种原因,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的代码时。没有映射-请设法一次又一次calloc用户

  

错误代码:检测到严重错误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';
    }
}

1 个答案:

答案 0 :(得分:5)

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

matrixData <- rep(c(TRUE, TRUE, FALSE), 8e7)
exampleMatrix <- matrix(matrixData, nrow=6e7, ncol=4, byrow=TRUE)

在之前的类似声明中,您正确地写了lengthStr = strlen(token); newUser->userName = (char*)calloc((lengthStr), sizeof(char)); ... strcpy(newUser->userName, token);

顺便说一句:在C语言中,通常不强制转换... = (char*)calloc((lengthStr+1), sizeof(char));的结果,根据定义,malloc始终为sizeof(char),并且不需要将内存设置为{{1 }},如果您仍然用后续的1填充内存,请使用0。所以你应该写...

calloc

请仔细阅读代码中的类似问题。