gpt4 book ai didi

c - Valgrind - 未初始化的值

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:45 25 4
gpt4 key购买 nike

以下代码是一个较大程序的一部分,因此我创建了一个测试文件来尝试找出问题所在。该代码完全按预期工作,但它抛出一个 valgrind 错误。根据我的理解,我认为它很可能指的是 *inputStr。

Valgrind 错误信息:

==2807== Conditional jump or move depends on uninitialised value(s)
==2807== at 0x3156434819: ____strtol_l_internal (in /lib64/libc-2.5.so)
==2807== by 0x3156431BD1: atoi (in /lib64/libc-2.5.so)
==2807== by 0x400818: getInt (test.c:50)
==2807== by 0x4008B5: main (test.c:70)
==2807== Uninitialised value was created by a stack allocation
==2807== at 0x400668: getInt (test.c:13)
==2807==
==2807== Conditional jump or move depends on uninitialised value(s)
==2807== at 0x315643482F: ____strtol_l_internal (in /lib64/libc-2.5.so)
==2807== by 0x3156431BD1: atoi (in /lib64/libc-2.5.so)
==2807== by 0x400818: getInt (test.c:50)
==2807== by 0x4008B5: main (test.c:70)
==2807== Uninitialised value was created by a stack allocation
==2807== at 0x400668: getInt (test.c:13)
==2807==
==2807== Use of uninitialised value of size 8
==2807== at 0x31564348A8: ____strtol_l_internal (in /lib64/libc-2.5.so)
==2807== by 0x3156431BD1: atoi (in /lib64/libc-2.5.so)
==2807== by 0x400818: getInt (test.c:50)
==2807== by 0x4008B5: main (test.c:70)
==2807== Uninitialised value was created by a stack allocation
==2807== at 0x400668: getInt (test.c:13)

我的代码:通过 stdin(例如“i 5”)解析输入命令,“i”将用于菜单开关(但该代码已删除),然后调用 getInt(),它开始扫描 inputStr索引 1 解析来自 inputStr 的整数值。为了清楚起见,我已将错误行 13、50 和 70 标记为注释。

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int getInt(char *inputStr, int *value, int *i) { // <-----line 13
char num[5];
int ctr = 0;

/* Skip whitespace */
while (!isdigit(*(inputStr+(*i))) && *(inputStr+(*i)) != '-') {
/* Input with only command letter and no integer value */
if (*(inputStr+(*i)) == '\n') {
//printError(-1);
return -1;
}
/* Input has invalid characters after command letter */
if (!isspace(*(inputStr+(*i)))) {
//printError(-2);
return -2;
}
(*i)++;
}

if (*(inputStr+(*i)) == '-') {
num[ctr++] = *(inputStr+(*i));
(*i)++;
}

/* Copy number characters to another array */
while (isdigit(*(inputStr+(*i)))) {
num[ctr++] = *(inputStr+(*i));
(*i)++;
}

/* Check if unwanted characters terminated the while loop above */
if (!isspace(*(inputStr+(*i)))) {
//printError(-2);
return -2;
}

/* Convert number characters to integer */
*value = atoi(num); // <----line 50

//printf("Positive Integer: num = %s, value = %d\n",num, value);
return 0;
}


int main () {
int value, i, int1;
char *inputStr;
size_t sizeInput = 10;
inputStr = malloc(sizeof(char) * sizeInput);

for (i = 0; i < sizeInput; i++)
inputStr[i] = '\0';

value = 0;
i = 1; //starting position for parsing number

getline(&inputStr, &sizeInput, stdin);
int1 = getInt(inputStr, &value, &i); // <---line 70
printf ("%d\n", value);

free(inputStr);
return 0;
}

感谢您的帮助。

[已解决]感谢@JonathanLeffler。在 atoi() 转换上方添加了 num[ctr] = '\0'; 一行。现在没有错误了。

最佳答案

你有多个错误:

  1. getline如果缓冲区不够大以容纳该行,则通过第一个参数返回新内存,因此您不需要为 inputStr 分配内存,但必须释放它。您可以将 inputStr 设置为 NULL,getline 将返回新的内存。

  2. 当您调用函数 getInt 时,变量“i”被初始化为 1; C/C++数组索引从0开始,所以应该是0,不知道你是不是打算把1作为第一个传过去。

  3. 在函数getInt中,如果while和if block 没有被执行,变量'num'没有被初始化,你可以把它改成:

    字符数[5] = {0};

  4. 同样在函数 getInt 中,没有代码检查字符串 inputStr 是否已经结束,因此您的循环可能会导致访问冲突。

关于c - Valgrind - 未初始化的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com