内存使用和valgrind

http://www.oschina.net/translate/valgrind-memcheck

 

1、使用未初始化的内存

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char *p;
 
    char c = *p;
 
    printf("\n [%c]\n",c);
 
    return 0;
}

  

root@ubuntu:/home/naviwork/valgrind_use# valgrind --tool=memcheck ./a.out
==2054== Memcheck, a memory error detector
==2054== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2054== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2054== Command: ./a.out
==2054==
==2054== Use of uninitialised value of size 4
==2054== at 0x80483F1: main (main.c:8)
==2054==

 2、在内存释放后读写

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char *p = malloc(1);
    *p = 'a';
 
    char c = *p;
 
    printf("\n [%c]\n",c);
 
    free(p);
    c = *p;
    return 0;
}

root@ubuntu:/home/naviwork/valgrind_use# valgrind --tool=memcheck ./a.out
==2131== Memcheck, a memory error detector
==2131== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2131== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2131== Command: ./a.out
==2131==

[a]
==2131== Invalid read of size 1
==2131== at 0x80484A5: main (main.c:14)
==2131== Address 0x4199028 is 0 bytes inside a block of size 1 free'd
==2131== at 0x4025FE9: free (vg_replace_malloc.c:446)
==2131== by 0x80484A0: main (main.c:13)

3. 从已分配内存块的尾部进行读/写

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char *p = malloc(1);
    *p = 'a';
 
    char c = *(p+1);
 
    printf("\n [%c]\n",c);
 
    free(p);
    return 0;
}

root@ubuntu:/home/naviwork/valgrind_use# valgrind --tool=memcheck ./a.out
==2153== Memcheck, a memory error detector
==2153== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2153== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2153== Command: ./a.out
==2153==
==2153== Invalid read of size 1
==2153== at 0x804847B: main (main.c:9)
==2153== Address 0x4199029 is 0 bytes after a block of size 1 alloc'd
==2153== at 0x40265DC: malloc (vg_replace_malloc.c:270)
==2153== by 0x8048468: main (main.c:6)
==2153==

4、内存泄漏

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char *p = malloc(1);
    *p = 'a';
 
    char c = *p;
 
    printf("\n [%c]\n",c);
 
    return 0;
}

root@ubuntu:/home/naviwork/valgrind_use# valgrind --tool=memcheck --leak-check=full ./a.out
==2179== Memcheck, a memory error detector
==2179== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2179== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2179== Command: ./a.out
==2179==

[a]
==2179==
==2179== HEAP SUMMARY:
==2179== in use at exit: 1 bytes in 1 blocks
==2179== total heap usage: 1 allocs, 0 frees, 1 bytes allocated
==2179==
==2179== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2179== at 0x40265DC: malloc (vg_replace_malloc.c:270)
==2179== by 0x8048428: main (main.c:6)
==2179==
==2179== LEAK SUMMARY:
==2179== definitely lost: 1 bytes in 1 blocks
==2179== indirectly lost: 0 bytes in 0 blocks
==2179== possibly lost: 0 bytes in 0 blocks
==2179== still reachable: 0 bytes in 0 blocks
==2179== suppressed: 0 bytes in 0 blocks
==2179==
==2179== For counts of detected and suppressed errors, rerun with: -v
==2179== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)

5. 不匹配地使用malloc/new/new[] 和 free/delete/delete[]

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
 
int main(void)
{
    char *p = (char*)malloc(1);
    *p = 'a';
 
    char c = *p;
 
    printf("\n [%c]\n",c);
    delete p;
    return 0;
}

root@ubuntu:/home/naviwork/valgrind_use# g++ main.c -g
root@ubuntu:/home/naviwork/valgrind_use# valgrind --tool=memcheck --leak-check=full ./a.out
==2201== Memcheck, a memory error detector
==2201== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2201== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2201== Command: ./a.out
==2201==

[a]
==2201== Mismatched free() / delete / delete []
==2201== at 0x4025BD6: operator delete(void*) (vg_replace_malloc.c:480)
==2201== by 0x804867F: main (main.c:13)
==2201== Address 0x42d3028 is 0 bytes inside a block of size 1 alloc'd
==2201== at 0x40265DC: malloc (vg_replace_malloc.c:270)
==2201== by 0x8048648: main (main.c:7)
==2201==
==2201==
==2201== HEAP SUMMARY:
==2201== in use at exit: 0 bytes in 0 blocks
==2201== total heap usage: 1 allocs, 1 frees, 1 bytes allocated
==2201==
==2201== All heap blocks were freed -- no leaks are possible
==2201==
==2201== For counts of detected and suppressed errors, rerun with: -v
==2201== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)

6. 两次释放内存

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char *p = (char*)malloc(1);
    *p = 'a';
 
    char c = *p;
    printf("\n [%c]\n",c);
    free(p);
    free(p);
    return 0;
}

root@ubuntu:/home/naviwork/valgrind_use# valgrind --tool=memcheck --leak-check=full ./a.out
==2221== Memcheck, a memory error detector
==2221== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2221== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2221== Command: ./a.out
==2221==

[a]
==2221== Invalid free() / delete / delete[] / realloc()
==2221== at 0x4025FE9: free (vg_replace_malloc.c:446)
==2221== by 0x804858B: main (main.c:12)
==2221== Address 0x42d3028 is 0 bytes inside a block of size 1 free'd
==2221== at 0x4025FE9: free (vg_replace_malloc.c:446)
==2221== by 0x804857F: main (main.c:11)
==2221==
==2221==
==2221== HEAP SUMMARY:
==2221== in use at exit: 0 bytes in 0 blocks
==2221== total heap usage: 1 allocs, 2 frees, 1 bytes allocated
==2221==
==2221== All heap blocks were freed -- no leaks are possible
==2221==
==2221== For counts of detected and suppressed errors, rerun with: -v
==2221== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)

 

6、内存重叠

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	char buf[12] = {0};
	
	memcpy(buf, buf + 5, 6);
	
    return 0;
}

root@ubuntu:/home/naviwork/valgrind_use# valgrind ./a.out 

==1886== Memcheck, a memory error detector
==1886== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==1886== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==1886== Command: ./a.out
==1886==
==1886== Source and destination overlap in memcpy(0xbe9306b0, 0xbe9306b5, 6)
==1886== at 0x4028693: memcpy (mc_replace_strmem.c:878)
==1886== by 0x804856F: main (main.c:9)

 

转载于:https://www.cnblogs.com/renhl/p/4312031.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GDB和Valgrind都是常用的调试工具,可以帮助开发人员找到程序中的错误。下面是它们的简单介绍以及如何使用它们进行调试的步骤: ## GDB GDB是GNU调试器,可以用来调试C和C++程序。下面是使用GDB进行调试的步骤: 1. 在编译时,需要在命令行加上-g选项,以生成调试信息。例如: ``` gcc -g -o program program.c ``` 2. 启动GDB,命令为: ``` gdb program ``` 3. 在GDB中运行程序,命令为: ``` (gdb) run ``` 4. 如果程序崩溃或停止了,可以使用backtrace命令查看函数调用栈: ``` (gdb) backtrace ``` 5. 使用print命令查看变量的值: ``` (gdb) print variable_name ``` 6. 使用break命令设置断点: ``` (gdb) break line_number ``` 7. 使用step命令单步执行代码: ``` (gdb) step ``` ## Valgrind Valgrind是一个内存调试和性能分析工具,可以检测内存泄漏、越界访问等问题。下面是使用Valgrind进行调试的步骤: 1. 在编译时,需要在命令行加上-g选项,以生成调试信息。例如: ``` gcc -g -o program program.c ``` 2. 启动Valgrind,命令为: ``` valgrind --tool=memcheck --leak-check=yes ./program ``` 3. Valgrind会在程序执行完成后输出内存错误信息。 ``` ==1234== Memcheck, a memory error detector ==1234== Copyright (C) ... ==1234== ... ==1234== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) ==1234== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) ``` 4. 如果有内存错误,Valgrind会输出详细的信息,包括错误堆栈和错误位置等。例如: ``` ==1234== Invalid read of size 4 ==1234== at 0x8048400: main (program.c:10) ==1234== Address 0x0 is not stack'd, malloc'd or (recently) free'd ==1234== ==1234== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) ==1234== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) ``` 可以看到,GDB和Valgrind都是非常有用的调试工具,可以帮助开发人员快速找到程序中的错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值