valgrind错误:Syscall param write(buf) points to uninitialised bytes(s)
来源:互联网 发布:java扑克牌发牌程序 编辑:程序博客网 时间:2024/06/05 08:43
最近使用valgrind检查代码时,发现了一个比较鬼诡的bug:Syscall param write(buf) points to uninitialised bytes(s) 。在百度上找了很长时间的解决方法,但没有找到。最后用google搞清楚了原因,并解决了这个问题。
这是我主要参考的英文网站:http://comments.gmane.org/gmane.comp.debugging.valgrind/7856
由于对齐等原因,我们定义变量等时应该先用memset对其进行初始化。然后再进行write、fwrite等调用。下面是一个具体的例子和其解决方法。
#include <stdio.h>#include <string.h>struct mystruct {char name[8];};int main(){struct mystruct s;FILE *f;strcpy(s.name, "12345");f = fopen("test.dat", "wb");fwrite(&s, 1, sizeof(s), f);fclose(f);return 0;}上述代码用valgrind运行时会报Syscall param write(buf) points to uninitialised bytes(s) 错误。原因是我们调用fwrite时是向其写入8个字节,而只有前6个字节被明确赋值了。
解决方法1:加入memset进行清0.
#include <stdio.h>#include <string.h>struct mystruct {char name[8];};int main(){struct mystruct s;memset(&s, 0, sizeof(s));FILE *f;strcpy(s.name, "12345");f = fopen("test.dat", "wb");fwrite(&s, 1, sizeof(s), f);fclose(f);return 0;}
解决方法2:用strlen(s.name + 1),刚好写入这么多字节数,代码如下:
#include <stdio.h>#include <string.h>struct mystruct {char name[8];};int main(){struct mystruct s;memset(&s, 0, sizeof(s));FILE *f;strcpy(s.name, "12345");f = fopen("test.dat", "wb");fwrite(&s, 1, sizeof(s.name) + 1, f);fclose(f);return 0;}这样问题就解决啦。。。
原创文章,转载请注明:http://blog.csdn.net/zhang2010kang
0 0
- valgrind错误:Syscall param write(buf) points to uninitialised bytes(s)
- valgrind提示的错误:Conditional jump or move depends on uninitialised value(s)
- HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)
- rsync 命令错误:writefd_unbuffered failed to write 1 bytes [sender]
- rsync 命令错误:writefd_unbuffered failed to write 1 bytes [sender]
- rsync 命令错误:writefd_unbuffered failed to write 1 bytes [sender] 【转】
- How to write the Gridview's RowCommand
- java HttpExchange返回中文报错 too many bytes to write to stream
- Conditional jump or move depends on uninitialised value(s)-1
- attempt to write a readonly database 错误
- Using valgrind to detect memory errors使用valgrind检测内存错误
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- python提示错误TypeError: write() argument must be str, not bytes
- Python 读写文件 错误TypeError: write() argument must be str, not bytes+
- SysCall
- Syscall
- 一例由于栈溢出导致valgrind报告Invalid write的错误
- Read and write bytes data
- 【暑期基础】H HDU 2007 平方和与立方和
- UVA 572 Oil Deposits
- 【2015/07/31】request.getParameter() 和request.getAttribute() 区别
- 字符串反转之——手摇算法
- 【HDU 5335】Walk Out(BFS)
- valgrind错误:Syscall param write(buf) points to uninitialised bytes(s)
- activity、fragment生命周期流程图
- js、URL传递含有中文参数时的乱码问题解决
- [leetcode]Find Minimum in Rotated Sorted Array II
- PriorityQueue使用示例介绍
- 多校第四场 1009 hdu 5335 Walk Out(bfs+贪心)
- Eclipse编译打包时报Conversion to Dalvik format failed with error 1 错误的解决方式
- POI3.1.2读取excel表示例
- 华为是怎么研发的(17)——组织氛围