gpt4 book ai didi

c - 大小 1 的写入无效

转载 作者:行者123 更新时间:2023-11-30 19:32:48 25 4
gpt4 key购买 nike

我已经修复了大部分分割函数,该函数根据参数将原始字符串分割成多个保存在字符串数组中的字符串:程序返回我想要的值,但 valgrind 给了我以下信息:

abc,defg
pasa
pasa
pasa
pasa
pasa
pasa
pasa
pasa
pasa
==2938== Conditional jump or move depends on uninitialised value(s)
==2938== at 0x4C2DB3C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2938== by 0x400912: split (strutil.c:31)
==2938== by 0x400A06: main (strutil.c:45)
==2938== Uninitialised value was created by a stack allocation
==2938== at 0x400723: split (strutil.c:9)
==2938==
==2938== Conditional jump or move depends on uninitialised value(s)
==2938== at 0x4C31577: __strncpy_sse2_unaligned (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2938== by 0x40097F: split (strutil.c:34)
==2938== by 0x400A06: main (strutil.c:45)
==2938== Uninitialised value was created by a stack allocation
==2938== at 0x400723: split (strutil.c:9)
==2938==
==2938== Conditional jump or move depends on uninitialised value(s)
==2938== at 0x4C31631: __strncpy_sse2_unaligned (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2938== by 0x40097F: split (strutil.c:34)
==2938== by 0x400A06: main (strutil.c:45)
==2938== Uninitialised value was created by a stack allocation
==2938== at 0x400723: split (strutil.c:9)
==2938==
==2938== Conditional jump or move depends on uninitialised value(s)
==2938== at 0x4C3164F: __strncpy_sse2_unaligned (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2938== by 0x40097F: split (strutil.c:34)
==2938== by 0x400A06: main (strutil.c:45)
==2938== Uninitialised value was created by a stack allocation
==2938== at 0x400723: split (strutil.c:9)
==2938==
==2938==
==2938== More than 10000000 total errors detected. I'm not reporting any more.
==2938== Final error counts will be inaccurate. Go fix your program!
==2938== Rerun with --error-limit=no to disable this cutoff. Note
==2938== that errors may occur in your program without prior warning from
==2938== Valgrind, because errors are no longer being displayed.
==2938==
abc
defg

我的代码是:

#include "strutil.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char** split(const char* str, char sep){
size_t cant = 2;
size_t i = 0;
for(i = 0; i < strlen(str); i++){
if(str[i] == sep)
cant ++;
}
size_t corte[cant];
corte[0] = 0;
size_t j = 1;
size_t cant_corte[cant];
for(i = 0; i < cant; i++)
cant_corte[i] = 0;
for(i = 0; i <= strlen(str); i++){
if(str[i] == sep || str[i] == '\0'){
corte[j] = i + 1;
cant_corte[j - 1] = corte[j] - corte[j - 1];
j++;
}
}

char** strv = malloc(sizeof(char*) * cant);
if (strv == NULL)return NULL;
for(i=0; i < cant; i++){
strv[i] = malloc(sizeof(char) * cant_corte[i]); //line 30
if (strv[i] == NULL)return NULL;
memcpy(strv[i], str + corte[i], cant_corte[i]);
strv[i][cant_corte[i] -1] = '\0'; //line 33
}
strv[cant - 1] = NULL;
return strv;
}

int main(){
char* eje = "abc,defg";
printf("%s\n", eje);
char r = ',';
char** prueba = split(eje, r);
printf("%s\n", prueba[0]);
printf("%s\n", prueba[1]);
getchar();
return 0;
}

我不知道问题出在哪里,因为它看起来与产生问题的值相同,但我似乎找不到它

编辑我编辑了代码,现在 valgrind 显示我:

==3751== Invalid write of size 1
==3751== at 0x4009DB: split (strutil.c:33)
==3751== by 0x400A64: main (strutil.c:43)
==3751== Address 0x520457f is 1 bytes before a block of size 0 alloc'd
==3751== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3751== by 0x400949: split (strutil.c:30)
==3751== by 0x400A64: main (strutil.c:43)

我尝试在第 30 行向 malloc 添加 +1,但是却说...block of size 1 aloc'd...我不知道,根据我的研究,它应该是这样。提前致谢

最佳答案

你最终得到cant_corte[i]0,然后你strv[i][cant_corte[i] -1] = '\0 ';

所以 strv[i][-1] 不是一个有效的写入地址。

我鼓励您学习如何将 valgrindgdb 一起使用,如 http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver-simple 中所述。

关于c - 大小 1 的写入无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46697273/

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