如何解决valgrind无效的读 /写入尺寸1&地址#是x Alloc' d的块之后的0字节

发布于 2025-01-22 07:48:58 字数 1301 浏览 0 评论 0原文

我看到了尺寸1&地址#是X Alloc'd的大块块非常普遍后,我无法在上下文中精确指出。

对于某些背景,我正在制作一个动态的字符串编码函数:

我声明了一个calloc的缓冲区: char * buffer = calloc(4,sizeof(char));

i声明另一个将构建编码字符串的变量为: char * encoded_str = calloc(5,sizeof(char));

缓冲区的字符串序列和返回的编码字符串应分别为3和4个字符。我每个关于此主题的帖子都会为每个角色填充一个

字符-1“> valgrind:尺寸1的读 /写入 /写作无效

我想象我所有的字符串将是零端的举动,但这并不能解决问题。

继续进行:

我将文件的3个字节读取到缓冲区中: fread(buffer,sizeof(char),3,in_file);

,我同样将其置于: buffer [3] ='\ 0';

我希望在buffer上调用我的encoder函数,并期望它将存储在Encoded_str中:这样: encoded_str = encoder(buffer);

但是,我的内存泄漏为:

==77225== HEAP SUMMARY:
==77225==     in use at exit: 5 bytes in 1 blocks
==77225==   total heap usage: 4 allocs, 3 frees, 582 bytes allocated

==74372== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==74372==    at 0x4C2C089: calloc (vg_replace_malloc.c:762)
==74372==    by 0x400C78: sendToEncoder (enc.c:139)

其中139行对应于char * encoded_str = calloc(5,sizeof(char));>

此ENCODED_ST是该函数的返回产品,在此通话中为了一致性,我将终止终止。

输出实际上只是“ fo”,输出是正确的。

有更多的复杂性,但是在这种最基本的情况下,该程序是错误的事实,告诉我,内存分配 /管理的一些基本方面我缺少,随着子句变得更加复杂和Encoded_str构建,这一事实正在传播。

I see this Valgrind issue of invalid read / write of size 1 & Address # is 0 bytes after a block of size x alloc'd is quite commonplace and I am unable to quite pinpoint in my context.

For some background, I am making a dynamic string encoding function:

I declare a calloc'd buffer as so:
char * buffer = calloc( 4, sizeof(char) );

I declare another variable that will hold the building encoding string as:
char * encoded_str = calloc(5, sizeof(char));

The string sequence of the buffer and the returned encoded string should be 3 and 4 chars respectively. I have alloc'd a one character padding each per the plethora of posts concerning this topic, such as:

Valgrind: Invalid read / write of size 1

I imagined that zero-terminating all my strings would be the move but it is not resolving the issue.

Proceeding onward:

I read 3 bytes of a file into the buffer as so:
fread( buffer, sizeof(char), 3, in_file );

and I similarly null-terminate it:
buffer[3] = '\0';

I have the desire to call my encoder function on buffer with the expectation that it will be stored in encoded_str as such:
encoded_str = encoder( buffer );

However, I have a memory leak as:

==77225== HEAP SUMMARY:
==77225==     in use at exit: 5 bytes in 1 blocks
==77225==   total heap usage: 4 allocs, 3 frees, 582 bytes allocated

==74372== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==74372==    at 0x4C2C089: calloc (vg_replace_malloc.c:762)
==74372==    by 0x400C78: sendToEncoder (enc.c:139)

where line 139 corresponds to the char * encoded_str = calloc(5, sizeof(char));

This encoded_str is the return product of the function and I null terminate after this call for consistency sake.

The output is correct where the input is literally just "fo".

There is a lot more complexity but the fact that the program is erroring in this most basic of cases tells me there is some basic aspect of memory allocation / management that I am missing that is propagating as the clauses get more complex and encoded_str builds.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文