一、若程序中存在迷途指针,轻则导致程序退出,重则使程序出现重大逻辑错误

    1、定义:内存已释放,指针依旧指向原始内存,这种指针就是迷途指针

    2、迷途指针和指针别名:

      1)、指针依旧指向已释放的内存,无法访问内存中的内容;

      2)、迷途指针没有指向有效对象,也称为内存过早释放;

      3)、两个指针指向同一个内存区域,称为指针别名;

      4)、使用指针别名的程序容易出现迷途指针,任意释放一个指针的内存即可,不需要每个都释放一下;

      5)、linux中使用工具valgrind,使用命令valgrind --tool=memcheck --leak-check=yes fileName检测程序fileName的内存泄露情况;

    3、代码形式:

      1)、指针依旧指向已释放的内存,无法访问内存中的内容;

int  *ptrInt1 = (int *)malloc(sizeof(int));
*ptrInt = ;free(ptrInt1);

*ptrInt1 = 10;

      2)、迷途指针没有指向有效对象,也称为内存过早释放;

int  *ptrInt1 = (int *)malloc(sizeof(int));
*ptrInt = ;
free(ptrInt1);

printf("%d",*ptrInt1);

      3)、两个指针指向同一个内存区域,称为指针别名;

int  *ptrInt1 = (int *)malloc(sizeof(int));
*ptrInt = ;

int *ptrInt2 = ptrInt1;

free(ptrInt1);

*ptrInt1 = ;

      4)、使用指针别名的程序容易出现迷途指针,任意释放一个指针的内存即可,不需要每个都释放一下;

int  *ptrInt1 = (int *)malloc(sizeof(int));
*ptrInt = ;

int *ptrInt2 = ptrInt1;

free(ptrInt1);

free(ptrInt2);

      5)、linux中使用工具valgrind,使用命令valgrind --tool=memcheck --leak-check=yes fileName检测程序fileName的内存泄露情况;

  #include <stdio.h>
#include <stdlib.h> int main(int argc, char **argv)
{
int *ptrInt = (int *)malloc(sizeof(int) * );
int size = ;
for(int i = ; i < size; i++){
*(ptrInt + i) = + i;
} for(int i = ; i < size; i++){
printf("ptrInt[%d]: %d\t", i, *(ptrInt + i));
}
free(ptrInt); return ;
}

  将上述代码编译后生成test14可执行文件,使用命令:valgrind --tool=memcheck --leak-check=yes test14,结果为

 develop  …  CodeStudy  cnblogs_understanding_and_using_c_pointers  chapter2  valgrind --tool=memcheck --leak-check=yes test14
==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.13. and LibVEX; rerun with -h for copyright info
==== Command: test14
====
ptrInt[]: ptrInt[]: ptrInt[]: ptrInt[]: ptrInt[]: ptrInt[]: ====
==== HEAP SUMMARY:
==== in use at exit: bytes in blocks
==== total heap usage: allocs, frees, , bytes allocated
====
==== All heap blocks were freed -- no leaks are possible
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )

  上述结果没有发生内存泄露,若将第15行代码注释掉,重新编译,执行命令:valgrind --tool=memcheck --leak-check=yes test14,结果为

cnblogs_understanding_and_using_c_pointers  chapter2  valgrind --tool=memcheck --leak-check=yes test14 ==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.13. and LibVEX; rerun with -h for copyright info
==== Command: test14
====
ptrInt[]: ptrInt[]: ptrInt[]: ptrInt[]: ptrInt[]: ptrInt[]: ====
==== HEAP SUMMARY:
==== in use at exit: bytes in blocks
==== total heap usage: allocs, frees, , bytes allocated
====
==== bytes in blocks are definitely lost in loss record of
==== at 0x4C2CE5F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==== by 0x1086B2: main (test14.c:)
====
==== LEAK SUMMARY:
==== definitely lost: bytes in blocks
==== indirectly lost: bytes in blocks
==== possibly lost: bytes in blocks
==== still reachable: bytes in blocks
==== suppressed: bytes in blocks
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )

  检查表明有一处内存泄露,有24个字节内存泄露,其他的请参看valgrind的帮助文档,man valgrind即可,非常方便。

  

16深入理解C指针之---迷途指针的更多相关文章

  1. C迷途指针

    在计算机编程领域中,迷途指针,或称悬空指针.野指针,指的是不指向任何合法的对象的指针. 当所指向的对象被释放或者收回,但是对该指针没有作任何的修改,以至于该指针仍旧指向已经回收的内存地址,此情况下该指 ...

  2. 迷途指针 new delete

    编程中有一种很难发现的错误是迷途指针.迷途指针也叫悬浮指针.失控指针,是党对一个指针进行delete操作后——这样会释放它所指向的内存——并没有把它设置为空时产生的.而后,如果你没有重新赋值就试图再次 ...

  3. void指针和NULL指针

    Void指针和NULL指针 Void指针: Void指针我们称之为通用指针,就是可以指向任意类型的数据.也就是说,任何类型的指针都可以赋值给Void指针. 举例: #include<stdio. ...

  4. 深入理解C语言中的指针与数组之指针篇

    转载于http://blog.csdn.net/hinyunsin/article/details/6662851     前言 其实很早就想要写一篇关于指针和数组的文章,毕竟可以认为这是C语言的根本 ...

  5. 深入理解C语言中的指针与数组之指针篇(转载)

    前言 其实很早就想要写一篇关于指针和数组的文章,毕竟可以认为这是C语言的根本所在.相信,任意一家公司如果想要考察一个人对C语言的理解,指针和数组绝对是必考的一部分. 但是之前一方面之前一直在忙各种事情 ...

  6. 深入理解C指针之五:指针和字符串

    原文:深入理解C指针之五:指针和字符串 基础概念 字符串可以分配到内存的不同区域,通常使用指针来支持字符串操作.字符串是以ASCII字符NUL结尾的字符序列.ASCII字符NUL表示为\0.字符串通常 ...

  7. 深入理解C指针之六:指针和结构体

    原文:深入理解C指针之六:指针和结构体 C的结构体可以用来表示数据结构的元素,比如链表的节点,指针是把这些元素连接到一起的纽带. 结构体增强了数组等集合的实用性,每个结构体可以包含多个字段.如果不用结 ...

  8. 深入理解 C/C++ 数组和指针

    本文转载自CSDN@WalkingInTheWind,原文链接:https://blog.csdn.net/luckyxiaoqiang/article/details/7044380 C语言中数组和 ...

  9. 关于C语言的指针数组与指针数组的个人理解

    一.指针数组与指针数组 1,指针数组 顾名思义,即一个元素全部是指针的数组,其形式与普通数组相似,形式如 *a[N]. 在理解指针数组的使用方式前,我先来说下我个人对数组的理解. 比如一维整形数组(形 ...

随机推荐

  1. Docker 自动运行Nginx容器

    Dockerfile文件如下: FROM ubuntu #基础镜像 RUN apt-get update #更新apt RUN apt-get -y install nginx #安装nginx VO ...

  2. python3.6:DLL load failed:找不到指定的模块(from PyQt5 import QtCore)

    本人小白搭建pyqt环境时遇到问题 运行代码 from PyQt5 import QtCore' 发现错误 ImportError: DLL load failed: 找不到指定的模块 这个问题折磨了 ...

  3. python操作Excel模块openpyxl

    https://www.cnblogs.com/zeke-python-road/p/8986318.html # -*- coding: utf-8 -*-from openpyxl import ...

  4. Pond Cascade Gym - 101670B 贪心+数学

    题目:题目链接 思路:题目让求最下面池子满的时间和所有池子满的时间,首先我们考虑所有池子满的时间,我们从上到下考虑,因为某些池子满了之后溢出只能往下溢水,考虑当前池子如果注满时间最长,那么从第一个池子 ...

  5. linux用户和用户组管理详解

    Linux 用户和用户组管理 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账号一方面可以帮助 ...

  6. CentOS-文件操作

    centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建文件夹 mkdir 文件名 新建一个名为test的文件夹在home下 view source1 mkdir ...

  7. luogu3178 [HAOI2015]树上操作

    裸题 #include <iostream> #include <cstdio> using namespace std; typedef long long ll; int ...

  8. kruskal - 倍增 - 并查集 - Luogu 1967 货车运输

    P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过 ...

  9. Ext.js二级联动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link href ...

  10. python列出指定目录下的所有目录和文件

    import os import docx def scanfile(rootdir): result = [] for f in os.walk(rootdir): for files in f[2 ...