【C语言复习(二十七)】野指针和内存操作分析

来源:互联网 发布:windows nt server 编辑:程序博客网 时间:2024/06/05 02:50

1、初识野指针

        野指针和内存操作分析野指针通常是因为指针变量中保存的值不是一个合法的内存地址而造成的;
        野指针不是NULL指针,是指向不可用内存的指针;

        NULL指针不容易用错,因为if语句很好判断一个指针是不是NULL;

2、野指针的由来

        局部指针变量没有被初始化;

#include <stdio.h>#include <string.h>struct Student{    char* name;    int number;};int main(){    struct Student s;    strcpy(s.name, "Hello Body"); // OOPS!    s.number = 99;    printf("%s\n",s.name);    return 0;}

        使用已经释放过后的指针;

#include <stdio.h>#include <malloc.h>#include <string.h>void func(char* p){    printf("%s\n", p);    free(p);}int main(){    char* s = (char*)malloc(5);        strcpy(s, <span style="font-family: Arial, Helvetica, sans-serif;">"Hello Body"</span>);        func(s);        printf("%s\n", s); // OOPS!        return 0;}

        指针指向的变量在指针之前被销毁;

#include <stdio.h>char* func(){    char p[] = <span style="font-family: Arial, Helvetica, sans-serif;">"Hello Body"</span><span style="font-family: Arial, Helvetica, sans-serif;">;</span>        return p;}int main(){    char* s = func();        printf("%s\n", s); // OOPS!        return 0;}

3、常见错误分析

3.1 非法内存操作

结构体成员指针未初始化
没有为结构体指针分配足够的内存空间

#include <stdio.h>#include <malloc.h>struct Demo{    int* p;};int main(){    struct Demo d1;    struct Demo d2;        int i = 0;        for(i=0; i<10; i++)    {        d1.p[i] = 0; // OOPS!    }        d2.p = (int*)calloc(5, sizeof(int));        for(i=0; i<10; i++)    {        d2.p[i] = i; // OOPS!    }        free(d2.p);        return 0;}

3.2 内存初始化分析

内存分配成功,但并未初始化

#include <stdio.h>#include <malloc.h>int main(){    char* s = (char*)malloc(10);        printf(s); // OOPS!        free(s);           return 0;}

3.3 内存越界

数组越界

#include <stdio.h>void f(int a[10]){    int i = 0;        for(i=0; i<10; i++)    {        a[i] = i; // OOPS!        printf("%d\n", a[i]);    }}int main(){    int a[5];        f(a);           return 0;}

3.4 内存泄露

#include <stdio.h>#include <malloc.h>void f(unsigned int size){    int* p = (int*)malloc(size*sizeof(int));    int i = 0;        if( size % 2 != 0 )    {        return; // OOPS!    }        for(i=0; i<size; i++)    {        p[i] = i;        printf("%d\n", p[i]);    }        free(p);}int main(){    f(9);    f(10);           return 0;}

3.5 多次指针释放

#include <stdio.h>#include <malloc.h>void f(int* p, int size){    int i = 0;        for(i=0; i<size; i++)    {        p[i] = i;        printf("%d\n", p[i]);    }        free(p);}int main(){    int* p = (int*)malloc(5 * sizeof(int));        f(p, 5);        free(p); // OOPS!           return 0;}

3.6使用已经释放的指针

#include <stdio.h>#include <malloc.h>void f(int* p, int size){    int i = 0;        for(i=0; i<size; i++)    {        printf("%d\n", p[i]);    }        free(p);}int main(){    int* p = (int*)malloc(5 * sizeof(int));    int i = 0;        f(p, 5);        for(i=0; i<5; i++)    {        p[i] = i; // OOPS!    }           return 0;}

4、错误防范准则

        使用malloc申请了内存后,应该立即检查指针值是否为NULL,防止使用值为NULL的指针;
        牢记数组长度,防止数组越界操作,考虑使用柔性数组;

        动态申请操作必须和释放操作匹配,防止内存泄露和多次释放;

        free指针之后必须立即赋值为NULL,防止误操作;

 

0 0
原创粉丝点击