Valgrind 在使用 realloc 函数时抱怨?
我已经实现了我自己版本的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您按值传递 src,xstrcat 正在处理并修改其本地副本。您对
src
所做的任何更改都不会反映在调用函数中。这种方法让 xstrcat 修改 main 的 src 变量。
引用一个经常提到的例子:
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.This approach lets xstrcat modify main's
src
variable.To quote a frequently mentioned sample: