Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行时检查失败#2-围绕变量';chars';腐败 char*putNum(节点*头) { char*str; int size=0; int指数=0; int lastNum,count=0; 节点*温度=头部->左侧; lastNum=temp->info; while(lastNum!=0) { lastNum=lastNum/10; 计数++; } 如果(头部->信息信息)-1; 尺寸*=5+计数; str=(char*)malloc(sizeof(char)*size); str[0]='-'; 索引++; } 其他的 { 尺寸=头部->信息-1; 尺寸*=5+计数; str=(char*)malloc(sizeof(char)*size); } 对于(inti=0;iinfo);i++) { 半焦[5]; itoa(临时->信息,字符,10); 对于(int j=0;j左侧; } 返回str; }_C_Visual Studio - Fatal编程技术网

运行时检查失败#2-围绕变量';chars';腐败 char*putNum(节点*头) { char*str; int size=0; int指数=0; int lastNum,count=0; 节点*温度=头部->左侧; lastNum=temp->info; while(lastNum!=0) { lastNum=lastNum/10; 计数++; } 如果(头部->信息信息)-1; 尺寸*=5+计数; str=(char*)malloc(sizeof(char)*size); str[0]='-'; 索引++; } 其他的 { 尺寸=头部->信息-1; 尺寸*=5+计数; str=(char*)malloc(sizeof(char)*size); } 对于(inti=0;iinfo);i++) { 半焦[5]; itoa(临时->信息,字符,10); 对于(int j=0;j左侧; } 返回str; }

运行时检查失败#2-围绕变量';chars';腐败 char*putNum(节点*头) { char*str; int size=0; int指数=0; int lastNum,count=0; 节点*温度=头部->左侧; lastNum=temp->info; while(lastNum!=0) { lastNum=lastNum/10; 计数++; } 如果(头部->信息信息)-1; 尺寸*=5+计数; str=(char*)malloc(sizeof(char)*size); str[0]='-'; 索引++; } 其他的 { 尺寸=头部->信息-1; 尺寸*=5+计数; str=(char*)malloc(sizeof(char)*size); } 对于(inti=0;iinfo);i++) { 半焦[5]; itoa(临时->信息,字符,10); 对于(int j=0;j左侧; } 返回str; },c,visual-studio,C,Visual Studio,大家好,我正在visual studio中学习c,我正在尝试创建一个函数,该函数接受一个带有长数字的双链接列表,并将数字放入字符串中。该错误在函数的最后一个“}”中弹出 如果temp->info大于9999或小于-999怎么办?欢迎使用SO。使用调试器执行单步操作、设置断点并查看调用堆栈,您应该能够自己跟踪这一点。这是学习成为一名开发人员的一个重要部分,所以你最好解决这个问题,而不是让我们帮你解决。临时->信息不应该超出范围,因为我列出了一个列表,它的信息中只有5位数字。我试着自己调试它,但找不

大家好,我正在visual studio中学习c,我正在尝试创建一个函数,该函数接受一个带有长数字的双链接列表,并将数字放入字符串中。该错误在函数的最后一个“}”中弹出

如果
temp->info
大于9999或小于-999怎么办?欢迎使用SO。使用调试器执行单步操作、设置断点并查看调用堆栈,您应该能够自己跟踪这一点。这是学习成为一名开发人员的一个重要部分,所以你最好解决这个问题,而不是让我们帮你解决。临时->信息不应该超出范围,因为我列出了一个列表,它的信息中只有5位数字。我试着自己调试它,但找不到问题,这就是为什么我来这里的原因
chars
只能容纳4个字符,因为终止NUL需要一个字符。
chars[5]
应足够大,以包含该整数类型的任何数字的最长文本形式。但是你可能会觉得
chars[11]
足以容纳
INT\u MAX
加上nul终止符,但
INT\u MIN
还需要一个减号。教训是不要吝啬/严格控制字符串长度:
chars[20]在8或16 Gb的机器上并不奢侈。
char* putNum(Node* head)
{
    char* str;
    int size = 0;
    int index = 0;
    int lastNum, count = 0;

    Node* temp = head->left;
    lastNum = temp->info;
    while (lastNum != 0)
    {
        lastNum = lastNum / 10;
        count++;
    }

    if (head->info < 0)
    {
        size = abs(head->info) - 1;
        size *= 5 + count;
        str = (char*)malloc(sizeof(char) * size);
        str[0] = '-';
        index++;
    }
    else
    {
        size = head->info - 1;
        size *= 5 + count;
        str = (char*)malloc(sizeof(char) * size);
    }


    for (int i = 0; i < abs(head->info); i++)
    {
        char chars[5];
        itoa(temp->info, chars, 10);
        for (int j = 0; j < strlen(chars); j++)
        {
            str[index++] = chars[j];
        }
        temp = temp->left;
    }
    return str;
}