valgrind 错误“大小 4 的无效读取"; [英] valgrind error "Invalid read of size 4"

查看:38
本文介绍了valgrind 错误“大小 4 的无效读取";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序

 int* fun1(void)
 {
    int n=9;
    int *pf=&n;
    cout<<*pf<<endl;
    return pf;
 }
 int main(int argc, char *argv[])
 {
    int *p=fun1();
    cout<<*p;
    return 0;
 }

程序的编译和运行不会出现任何问题,但使用 valgrind 时会出现消息/警告大小 4 的无效读取".

Compilation and running of program doesn't give any problems but with valgrind it gives message/warning "Invalid read of size 4".

欢迎任何解决警告的帮助

Any help to resolve the warning is most welcome

推荐答案

nfun1()中的局部变量,退出函数后不再有效.

nis a local variable in fun1() and is no more valid after exit of the function.

这篇关于valgrind 错误“大小 4 的无效读取";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆