valgrind

Ubuntu 安装Codeblocks

匿名 (未验证) 提交于 2019-12-02 23:49:02
安装步骤:   一:首先安装简版CodeBlocks sudo apt install codeblocks   二:把编译环境,C库、C++库和Boost库装好 sudo apt install build-essential   三:安装 CodeBlocks 的调试组件 Valgrind 用来探测内存泄露的 sudo apt install valgrind   四:安装CodeBlocks常用插件 sudo apt install codeblocks-contrib   一:首先安装简版CodeBlocks sudo apt install codeblocks   二:把编译环境,C库、C++库和Boost库装好 sudo apt install build-essential   三:安装 CodeBlocks 的调试组件 Valgrind 用来探测内存泄露的 sudo apt install valgrind   四:安装CodeBlocks常用插件 sudo apt install codeblocks-contrib

Valgrind检测非法访问内存

匿名 (未验证) 提交于 2019-12-02 23:34:01
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/90311664 #include<iostream> #include<stdlib.h> using namespace std; void func(){ int *x=(int *)malloc( 10 * sizeof ( int ) ) ; x[10]=0; } int main(){ func(); cout<<"done"<<endl; return 0; } [root@localhost charpter05]# g++ -g 0508test.cpp -o 0508 [root@localhost charpter05]# ./0508 done [root@localhost charpter05]# valgrind ./0508 ==16719== Memcheck, a memory error detector ==16719== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==16719== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright

Valgrind检测内存读写越界

匿名 (未验证) 提交于 2019-12-02 23:34:01
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/90311919 内存读写越界是指访问了没有权限访问的内存地址空间,比如访问数组时越界,对动态内存访问超出了申请时内存的大小范围。 #include<stdlib.h> #include<iostream> using namespace std; int main(){ int len=4; int *pt=(int *)malloc(len*sizeof(int)); int *p=pt; for(int i=0;i<len;i++) p++; *p=5; cout<<"the value of p is "<<*p<<endl; return 0; } [root@localhost charpter05]# g++ -g 0511.cpp -o 0511 [root@localhost charpter05]# ./0511 the value of p is 5 [root@localhost charpter05]# valgrind ./0511 ==18335== Memcheck, a memory error detector ==18335== Copyright (C) 2002-2017, and

linux core dump 文件 gdb分析

匿名 (未验证) 提交于 2019-12-02 21:59:42
转载: https://www.cnblogs.com/bodhitree/p/5850212.html core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump. (linux中如果内存越界会收到SIGSEGV信号,然后就会core dump) 在程序运行的过程中,有的时候我们会遇到Segment fault(段错误)这样的错误。这种看起来比较困难,因为没有任何的栈、trace信息输出。该种类型的错误往往与指针操作相关。往往可以通过这样的方式进行定位。 一 造成segment fault,产生core dump的可能原因 1.内存访问越界 2 多线程程序使用了线程不安全的函数。 3 多线程读写的数据未加锁保护。对于会被多个线程同时访问的全局数据,应该注意加锁保护,否则很容易造成core dump 4 非法指针 a) 使用空指针 b) 随意使用指针转换。一个指向一段内存的指针,除非确定这段内存原先就分配为某种结构或类型,或者这种结构或类型的数组,否则不要将它转换为这种结构或类型的指针,而应该将这段内存拷贝到一个这种结构或类型中,再访问这个结构或类型。这是因为如果这段内存的开始地址不是按照这种结构或类型对齐的,那么访问它时就很容易因为bus error而core dump. 5

Valgrind errors caused by pclose() on Mac OS X

邮差的信 提交于 2019-12-02 21:20:51
问题 I'm getting valgrind errors when attempting to pclose() a pipe previously open with popen() . The errors occur on Mac OS X, but not on Linux. Consider the following example: #include <stdlib.h> #include <stdio.h> int main() { FILE *fp; char buf[4096]; if (!(fp = popen("ls", "r"))) exit(-1); while (fscanf(fp, "%s", buf) == 1) printf("%s\n", buf); pclose(fp); return 0; } I get the following valgrind errors on a Mac (OS X 10.6.7, valgrind version 3.6.0), except if I remove the pclose() call: =

finding a memory allocation error with omnet++

浪子不回头ぞ 提交于 2019-12-02 21:17:33
问题 I am doing networks simulations under omnet++ & veins(v2.0-rc1), for this purpose I experiment 4 scenarios with 20 repetitions for both one. I am getting some errors (i guess that is a memory allocation error) during the simulations of the 3rd & 4th scenarios wich share a common functionnality (ack). For the concerned simulations, it starts normally and executes a few repetition but failed after that (for example, it fails at the 5th repetition of the 4th scenario), here is the output : 0: **

Memory leak C++

谁说胖子不能爱 提交于 2019-12-02 20:00:24
I just wrote a code in C++ which does some string manipulation, but when I ran valgrind over, it shows some possible memory leaks. Debugging the code to granular level I wrote a simple C++ program looking like: #include<iostream> #include<cstdlib> using namespace std; int main() { std::string myname("Is there any leaks"); exit(0); } and running valgrind over it I got: ==20943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 26 from 1) ==20943== malloc/free: in use at exit: 360,645 bytes in 12,854 blocks. ==20943== malloc/free: 65,451 allocs, 52,597 frees, 2,186,968 bytes allocated. =

Valgrind does not report memory leak on “delete array”

筅森魡賤 提交于 2019-12-02 18:41:22
问题 After implementing the C++ code below, I ran valgrind --leak-check=full in order to check if there was any memory leak. The result was 0 bytes were in use at exit and no leaks are possible . However, later I discovered that I've forgotten to use delete[] x instead of just delete x inside the destructor. I searched for some explanations (for instance: delete vs delete[] operators in C++), and everything I read said that using delete without [] can cause memory leak, since it calls just the

c++ memory error when using malloc/realloc/free on std::string

…衆ロ難τιáo~ 提交于 2019-12-02 18:24:35
问题 I wrote a small piece of code like this: template <class T> void test() { T* ptr = nullptr; ptr = (T*)malloc(1 * sizeof(T)); new ((void*)ptr) T(T()); ptr = (T*)realloc(ptr, 2 * sizeof(T)); new ((void*)(ptr + 1)) T(T()); (ptr)->~T(); (ptr + 1)->~T(); free(ptr); } struct foo { foo() : ptr(malloc(10)) {} ~foo() { free(ptr); } void* ptr; }; int main() { test<int>(); // this is ok test<foo>(); // this is ok test<std::string>(); // memory error :( return 0; }; When T is [int] or [foo], everything

How do I make ctest run a program with valgrind without dart?

筅森魡賤 提交于 2019-12-02 17:34:15
I want to write a CMakeLists.txt so that I can run my tests normally or with valgrind. I have seen much on integrating ctest with valgrind but all with the assumption that you want to set up a server to submit test results to a dart dashboard. I just want to run the tests on my machine and see the results on the command line. If I have to do a cmake -D VALGRIND=ON thats fine, but I'd rather generate tests named "foo" and "valgrind_foo" if possible. KlingonJoe I use valgrind for my memory check. To configure valgrind, I define the following variables in my build system: find_program(