写入大小为1无效

时间:2017-10-11 20:47:09

标签: c split valgrind

我修复了我的大部分分割函数,它根据参数将原始字符串拆分为多个字符串数组中保存的字符串:程序返回我想要的值,但是valgrind用以下内容命中我: / p>

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,而是说...大小为1的块aloc&d; d ...我不知道并且从我研究过它应该是那样的。提前谢谢

1 个答案:

答案 0 :(得分:0)

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

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

我鼓励您按照http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver-simple

中的解释,了解如何valgrindgdb一起使用