简体   繁体   English

realloc,二维分配,valgrind中的泄漏和错误

[英]realloc, two dimention allocation, leak and errors in valgrind

  • I wrote a program to split given string according to certain delimiter.我编写了一个程序来根据某些分隔符拆分给定的字符串。 Everything works fine but there are leak and error in valgrind .一切正常,但valgrind存在泄漏和错误

  • split algorithm is correct. split算法是正确的。

  • substr works fine. substr工作正常。

  • My program :我的程序

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

char** split(const char*, char, int*);
char* substr(const char*, int, int);
void freepath(char**, int);

int main(void) {
    char *str = "home///ubuntu//Desktop";
    char **path = NULL;
    int size = 0;

    path = split(str, '/', &size);

    freepath(path, size);
    return 0;   
}

char** split(const char *str, char c, int *size) {

    char **path = NULL;
    const char *save = str;
    int from=-1, i;

    if(str == NULL)
        return NULL;

    for(i=0 ; 1; ++i) {
        if(*str == '\0') {
            if(from != -1) {
                ++(*size);
                path = (char**)realloc(path, (sizeof(char**) *(*size)));
                *(path+(*size)-1) = substr(save, from, i);
            }
            break;
        }
        if(*str != '/') {
            if(from == -1)
                from = i;
        }
        else {
            if(from != -1) {
                ++(*size);
                path = (char**)realloc(path, (sizeof(char)*(*size)));
                *(path+(*size)-1) = substr(save, from, i);
            }
            from = -1;
        }
        ++str;
    }
    return path;
}

void freepath(char **path, int size) {

    int i=0;

    for(i=0; i<size; ++i) {
        free(*(path+i));
        *(path+i) = NULL;
    }
    free(path);
    path = NULL;
}


char* substr(const char *src, int m, int n)
{
    int len = n - m;
    char *dest = (char*)malloc(sizeof(char) * (len + 1));

    for (int i = m; i < n && (*(src + i) != '\0'); i++)
    {
        *dest = *(src + i);
        ++dest;
    }
    *dest = '\0';

    return dest - len;
}
  • Valgrind output : Valgrind output 在此处输入图像描述
  • What should be the reason?应该是什么原因? , I really stuck with it ! ,我真的坚持了!

clang analyser has found 4 suspected points in your code: clang 分析器在您的代码中发现了 4 个可疑点:

1. 1.

char *str = "home///ubuntu//Desktop";

needs const in front of char (pointer to const).char前面需要const (指向 const 的指针)。

2. 2.

char** split(const char *str, char c, int *size) {

contains an unused parameter ( c ).包含一个未使用的参数( c )。

3. 3.

path = (char**)realloc(path, (sizeof(char**) *(*size)));

clang-analyser does not like char** as the argument of sizeof , replacing it with char* removes the warning. clang-analyser 不喜欢char**作为sizeof的参数,用char*替换它会删除警告。

4. 4.

path = (char**)realloc(path, (sizeof(char)*(*size)));

The same warning as in 3. Errr, no, not the same.与 3 中相同的警告。错误,不,不一样。 Bingo!答对了! Replace char inside sizeof with char* and you're back home.sizeof中的 char 替换为char char*即可。

One final remark.最后一句话。 When you use valgrind, always add debugging information to the compiled code, that is, add -g to the compiler command-line options (gcc, clang, etc.).使用 valgrind 时,一定要在编译后的代码中添加调试信息,即在编译器命令行选项(gcc、clang 等)中添加-g This will give you the information about the exact lines numbers in your source code corresponding to the places where the problem was spotted by valgrind.这将为您提供有关源代码中与 valgrind 发现问题的位置相对应的确切行号的信息。 My screenshot of your program under valgrind contains more information than yours:我在 valgrind 下的程序截图包含的信息比你的多:

valgrind 截图

Please notice that valgrind correctly identifies line 44 as the line with the buggy memory allocation (or line 45 with a buggy usage of the buffer allocated at line 44. Both options are a priori possibly correct).请注意,valgrind 正确地将第 44 行识别为带有错误 memory 分配的行(或第 45 行,在第 44 行分配的缓冲区使用错误。这两个选项都是先验可能正确的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM