16深入理解C指针之---迷途指针

时间:2022-04-05 05:57:24

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

    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即可,非常方便。