Valgrind 在使用 realloc 函数时抱怨?

发布于 2024-10-21 20:37:04 字数 1574 浏览 3 评论 0原文

我已经实现了我自己版本的 strcat 函数。
它工作正常,但 valgrind 抱怨。

main()
{
    char *src=NULL;
    src=(char *) malloc(sizeof(char)*8);
    strcpy(src,"sandeep");
    xstrcat(src,"pathak");
    printf("******FINAL STR IS : %s ********",src);
    free(src);
    src=NULL; 
}

void  xstrcat(char *src,const char *dest)
{
        int dlen=strlen(dest);
        int slen=strlen(src);
        int j=0;
        int i=0;
        char *temp=(char *) realloc(src,(slen+dlen+1)*sizeof(char));
        for(j=slen;i<dlen;i++,j++)
        {
                temp[j]=dest[i];
        }
        temp[j]='\0';
        printf("%s",temp);
}

VALGRIND 错误:

==31775== Invalid read of size 4
==31775== Invalid read of size 4
==31775== Invalid read of size 1
==31775== Invalid read of size 1
==31775== 14 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31775==    at 0x1B9053EE: realloc (vg_replace_malloc.c:197)
==31775==    by 0x8048606: xstrcat (in /user/gur27268/Computer_Systems/Socket/DevTest/UTI
==31775==    by 0x804850F: main (in /user/gur27268/Computer_Systems/Socket/DevTest/UTIL/a
==31775==
==31775== LEAK SUMMARY:
==31775==    definitely lost: 14 bytes in 1 blocks.
==31775==    possibly lost:   0 bytes in 0 blocks.
==31775==    still reachable: 0 bytes in 0 blocks.
==31775==         suppressed: 0 bytes in 0 blocks.
==31775== Reachable blocks (those to which a pointer was found) are not shown.**
==31775== To see them, rerun with: --show-reachable=yes

我已经释放了 src,但是使用 realloc 会出现这个问题...

任何帮助将不胜感激..

I have implemented my own version of strcat function.
It works fine but valgrind complains.

main()
{
    char *src=NULL;
    src=(char *) malloc(sizeof(char)*8);
    strcpy(src,"sandeep");
    xstrcat(src,"pathak");
    printf("******FINAL STR IS : %s ********",src);
    free(src);
    src=NULL; 
}

void  xstrcat(char *src,const char *dest)
{
        int dlen=strlen(dest);
        int slen=strlen(src);
        int j=0;
        int i=0;
        char *temp=(char *) realloc(src,(slen+dlen+1)*sizeof(char));
        for(j=slen;i<dlen;i++,j++)
        {
                temp[j]=dest[i];
        }
        temp[j]='\0';
        printf("%s",temp);
}

VALGRIND ERROR :

==31775== Invalid read of size 4
==31775== Invalid read of size 4
==31775== Invalid read of size 1
==31775== Invalid read of size 1
==31775== 14 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31775==    at 0x1B9053EE: realloc (vg_replace_malloc.c:197)
==31775==    by 0x8048606: xstrcat (in /user/gur27268/Computer_Systems/Socket/DevTest/UTI
==31775==    by 0x804850F: main (in /user/gur27268/Computer_Systems/Socket/DevTest/UTIL/a
==31775==
==31775== LEAK SUMMARY:
==31775==    definitely lost: 14 bytes in 1 blocks.
==31775==    possibly lost:   0 bytes in 0 blocks.
==31775==    still reachable: 0 bytes in 0 blocks.
==31775==         suppressed: 0 bytes in 0 blocks.
==31775== Reachable blocks (those to which a pointer was found) are not shown.**
==31775== To see them, rerun with: --show-reachable=yes

I have freed src, but using realloc tends to this problem...

Any help would be appreciated..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

司马昭之心 2024-10-28 20:37:04
void xstrcat(char *src,const char *dest) {

您按值传递 src,xstrcat 正在处理并修改其本地副本。您对 src 所做的任何更改都不会反映在调用函数中。

void xstrcat(char **src,const char *dest) {
  // Work with *src
}

...
xstrcat(&src, ...)

这种方法让 xstrcat 修改 main 的 src 变量。

引用一个经常提到的例子:

void foo (int x) {
  x = 2;
}
void bar (int * x) {
  *x = 2;
}
int main() {
  foo(9); // foo cannot modify the literal 9
  int i = 1;
  foo(i); // foo cannot modify the variable i
  bar(&9); // This isn't legal - it can't be
  bar(&i); // bar modifies i
}
void xstrcat(char *src,const char *dest) {

You're passing src by value, and xstrcat is working on and modifying its local copy. Any changes you make to src will not be reflected in the calling function.

void xstrcat(char **src,const char *dest) {
  // Work with *src
}

...
xstrcat(&src, ...)

This approach lets xstrcat modify main's src variable.

To quote a frequently mentioned sample:

void foo (int x) {
  x = 2;
}
void bar (int * x) {
  *x = 2;
}
int main() {
  foo(9); // foo cannot modify the literal 9
  int i = 1;
  foo(i); // foo cannot modify the variable i
  bar(&9); // This isn't legal - it can't be
  bar(&i); // bar modifies i
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文