zoukankan      html  css  js  c++  java
  • 关于malloc问题的改错笔试常考

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://liangbing8612.blog.51cto.com/2633208/697911

    情形1

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. void GetMemory(char *p)   
    6. {  
    7.     p=(char *)malloc(100);  
    8. }  
    9. void Test(void)   
    10. {  
    11.     char *str=NULL;  
    12.     GetMemory(str);  
    13.     strcpy(str,"helloworld");  
    14.     printf("%s\n",str);  
    15. }   
    16.  
    17. int main(void)  
    18. {     
    19.     Test();  
    20.     return 0;  

    错误:
    1. 调用GetMemory(str),属传值,str并未变化,仍为NULL
    2. 调用strcpy,程序到这将产生错误,因为str并未指定某一内存。
    3. malloc内存时可能会出错,需判断内存是否申请成功,加上:
        if(*p==NULL)
        {
            进行申请内存失败处理
        }
    4. 动态创建的内存没释放
    修改方法1:
        传址-----使用指向指针的指针

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. void GetMemory(char **p)   
    6. {  
    7.     *p=(char *)malloc(100);  
    8. }  
    9. void Test(void)   
    10. {  
    11.     char *str=NULL;  
    12.     GetMemory(&str);  
    13.     strcpy(str,"helloworld");  
    14.     printf("%s\n",str);  
    15. }   
    16.  
    17. int main(void)  
    18. {     
    19.     Test();  
    20.     return 0;  

     修改方法2:

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. char* GetMemory()   
    6. {  
    7.     char* p=(char *)malloc(100);  
    8.     return p;  
    9. }  
    10. void Test(void)   
    11. {  
    12.     char *str=NULL;  
    13.     str=GetMemory();  
    14.     strcpy(str,"helloworld");  
    15.     printf("%s\n",str);  
    16. }   
    17.  
    18. int main(void)  
    19. {     
    20.     Test();  
    21.     return 0;  

     情形2

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. char *GetMemory(void)  
    6. {  
    7.     char p[]="hello world";  
    8.     return p;  
    9. }  
    10. void Test(void)  
    11. {  
    12.     char *str=NULL;  
    13.     str=GetMemory();  
    14.     printf("%s\n",str);  
    15. }  
    16. int main(void)  
    17. {     
    18.     Test();  
    19.     return 0;  

    错误原因:
        函数GetMemory中数组p为局部变量,在函数返回后,内存已经被释放,生存期结束。

    情形3

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. void GetMemory(char **p,int num)   
    6. {  
    7.     *p=(char *)malloc(num);    
    8. }  
    9. void Test(void)   
    10. {  
    11.     char *str=NULL;  
    12.     GetMemory(&str,100);  
    13.     strcpy(str,"helloworld");  
    14.     printf("%s\n",str);  
    15. }    
    16.    
    17. int main(void)  
    18. {     
    19.     Test();  
    20.     return 0;  

    可以打印出字符,但存在问题有:
        1. 函数GetMemory中,malloc内存后,未判断内存是否申请成功,应加上:
        if(*p==NULL)
        {
            //进行申请内存失败处理
        }
        2. 在Test函数中,未对malloc的内存进行释放。

    情形4:

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. void Test(void)   
    6. {  
    7.     char *str=(char*)malloc(100);  
    8.     strcpy(str,"helloworld");  
    9.     printf("%s\n",str);  
    10.     free(str);  
    11. }    
    12.    
    13. int main(void)  
    14. {     
    15.     Test();  
    16.     return 0;  
    17. }  

    存在问题有:
        1. malloc内存后,未判断内存是否申请成功
        2. 在free(str)后未置str为空,导致可能变成一个“野”指针,应加上:str=NULL

    情形5

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. void Test(void)   
    6. {  
    7.     char *str=(char*)malloc(100);  
    8.     strcpy(str,"hello");  
    9.     //str+=6;  
    10.     free(str);  
    11.     if(str!=NULL)  
    12.     {  
    13.         strcpy(str,"world");  
    14.         printf(str);  
    15.     }  
    16. }    
    17.    
    18. int main(void)  
    19. {     
    20.     Test();  
    21.     return 0;  

    在vc中可以输出"world",但这是不确定的值,存在的问题有:
        申请空间,拷贝字符串,释放空间.前三步操作都没有任何问题。到if语句里的判断条件开始出错了,因为一个指针被释放之后其内容并不是NULL,而是一个不确定的值。所以if语句永远都不能被执行.这也是著名的"野"指针问题.所以我们在程序中当释放一个指针后,一定要人为的将指针付成NULL。这样就会避免出现"野"指针的出现。有人说"野"指针很可怕,会带来意想不到的错误。

    情形6

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. void swap(int *p1,int *p2)   
    6. {  
    7.      int *p;  
    8.      *p=*p1;  
    9.      *p1=*p2;  
    10.      *p2=*p;  
    11. }    
    12.    
    13. int main(void)  
    14. {     
    15.     int a=3;  
    16.     int b=5;  
    17.     swap(&a,&b);  
    18.     printf("a=%d,b=%d\n",a,b);  
    19.     return 0;  

        在swap函数中,p是一个“野”指针,未指向任何内存,所以当将值*p1赋给*p时,值*p1无处存放,会出错。有可能指向系统区,导致程序运行崩溃。
        可修改为:

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.  
    5. void swap(int *p1,int *p2)   
    6. {  
    7.      int p;  
    8.      p=*p1;  
    9.      *p1=*p2;  
    10.      *p2=p;  
    11. }    
    12.    
    13. int main(void)  
    14. {     
    15.     int a=3;  
    16.     int b=5;  
    17.     swap(&a,&b);  
    18.     printf("a=%d,b=%d\n",a,b);  
    19.     return 0;  


     
     

    本文出自 “凉冰” 博客,请务必保留此出处http://liangbing8612.blog.51cto.com/2633208/697911

  • 相关阅读:
    VSS使用
    Delphi简单数据库连接程序
    为表增加字段与拷贝数据到另一个表
    VSTS 使用
    Delphi实现个相似的功能界面共用一个窗体
    看代码笔记
    数据库安全管理
    函数
    【USACO】Ordered Fractions 顺序的分数
    C# 专业数据库连接配置界面
  • 原文地址:https://www.cnblogs.com/ligun123/p/2228417.html
Copyright © 2011-2022 走看看