CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

一道笔试题,求解

楼主xulecn(流氓兔)2006-12-02 20:29:40 在 C/C++ / C++ 语言 提问

#include<iostream>  
  #include<cstring>  
  #include<malloc.h>  
   
  using   namespace   std;  
   
  int   main()  
  {  
  char   *   str=(*char)malloc(20*sizeof(char));  
  strcpy(str,"hello");  
  free(str);  
   
  if(str!=NULL)  
  {  
  strcpy(str,"word");  
  cout<<str;  
  }  
   
  return   0;  
  }  
   
  问输出是什么?  
  程序大概是这个样子的,但我调试通不过,不知哪错了?  
  该正后输出该是什么?  
  中兴的笔试题 问题点数:5、回复次数:23Top

1 楼abblly(西边日出东边雨)回复于 2006-12-02 20:45:35 得分 0

free(str);  
  已经free了,还用  
  f(str!=NULL)  
  {  
  strcpy(str,"word");  
  cout<<str;  
  }  
  这个就是野指针了,不出错才怪呢Top

2 楼sms88(白板http://shop34112882.taobao.com)回复于 2006-12-02 20:58:43 得分 0

他是让你上机做题?Top

3 楼xulecn(流氓兔)回复于 2006-12-02 21:07:15 得分 0

不是,是笔试题,问输出结果是什么?Top

4 楼hardworkerhw()回复于 2006-12-02 21:32:29 得分 0

程序改一下:  
  #include<iostream>  
  #include<cstring>  
  #include<malloc.h>  
   
  using   namespace   std;  
   
  int   main()  
  {  
  char   *   str=(*char)malloc(20*sizeof(char));  
  strcpy(str,"hello");  
  free(str);  
   
  if(str!=NULL)  
  {  
  strcpy(str,"word");  
  cout<<str;  
  }  
   
  return   0;  
  }  
   
   
  在VC里运行结果是word,是野指针,也不等于NULL。Top

5 楼xulecn(流氓兔)回复于 2006-12-02 22:10:40 得分 0

我在VC下调试的也是输出world;但出现异常  
  在linux下也可以输出world,但是没有异常  
  我想问题出在free()的实现上,请高手解释一下free(str)后,str是否还存在;Top

6 楼besthyq(寻欢醉一场,但愿长醉不愿醒!)回复于 2006-12-02 22:13:02 得分 1

str本身是还在,但是它指向的内存单元已经被释放了  
   
  这个时候,不能知道str指向的是什么内存单元了  
   
  也就是所谓的野指针了Top

7 楼Shadow_Mor()回复于 2006-12-02 22:32:51 得分 1

char   *   str=(*char)malloc(20*sizeof(char));  
   
  改成  
   
  char   *   str=(char*)malloc(20*sizeof(char));Top

8 楼loveliu0429(lovelipei)回复于 2006-12-02 22:42:50 得分 1

输出应该是word  
  但是这样用是不对的,即所谓野指针了  
  要想杜绝这种现象,要每次free(str)后,都要人为将str   =   NULL;  
  这样以后的防错判断才能起作用Top

9 楼qqtcc()回复于 2006-12-02 23:20:36 得分 1

#include<iostream>  
  #include<cstring>  
  #include<malloc.h>  
  using   namespace   std;  
   
  int   main()  
  {  
  char   *   str=(char   *)malloc(20*sizeof(char));  
                    //上一句不用改也能编译吗?原来是(*char)malloc(...)  
  strcpy(str,"hello");  
  free(str);  
  if(str!=NULL)  
  {  
  strcpy(str,"word");  
  cout<<str;  
  }  
  return   0;  
  }  
   
  结果:word  
  Top

10 楼qqtcc()回复于 2006-12-02 23:31:46 得分 1

#include<iostream>  
  #include<cstring>  
  #include<malloc.h>  
  #include<stdio.h>  
  using   namespace   std;  
   
  int   main()  
  {  
  char   *   str=(char   *)malloc(20*sizeof(char));  
  strcpy(str,"hello");  
  printf("%0x\n",str);     //释放之前指针指向单元  
   
  free(str);  
  //cout<<str<<endl;       //输出为乱码  
  //str=NULL;  
  printf("%0x\n",str);   //释放之后指针指向单元,VC下两个地址是一样的,只是单元不可用.  
  if(str!=NULL)  
  {  
  strcpy(str,"word");    
  cout<<str<<endl;  
  }  
  return   0;  
  }  
  结果:  
   
  441b20      
  441b20  
  word  
   
  其实,并所谓的"野指针"还是指向原来的内存单元,只是那个单元被释放了不可用了,指针变量本身还是在其的生命期之内.同样可以对指针变量赋值,指向别的单元.Top

11 楼hertz2007()回复于 2006-12-02 23:33:37 得分 0

首先,弄清free的功能,是否释放指针指向的空间,是否对指针赋值NULL。  
  这样才能判断执行if语句时,str是否为无意义指针。而且,一旦释放str后,不可能再对其赋字符串,因为没有申请新的空间。  
  这是我的看法,不知道高手以为如何?Top

12 楼msccao()回复于 2006-12-03 00:29:58 得分 0

我也很疑问,没有了空间,如何可以再赋值?请高手解答Top

13 楼argenCHN(【夷不谋夏,胡不乱华】)回复于 2006-12-03 00:38:05 得分 0

free之后   只是告诉操作系统那片内存区域可以释放了,但指针并没有置空(NULL)  
  所以还可以重新赋值,但这样是不安全的Top

14 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2006-12-03 00:56:43 得分 0

MSDN中free的一些解释,大家可以看看。  
  Remarks  
  The   free   function   deallocates   a   memory   block   (memblock)   that   was   previously   allocated   by   a   call   to   calloc,   malloc,   or   realloc.   The   number   of   freed   bytes   is   equivalent   to   the   number   of   bytes   requested   when   the   block   was   allocated   (or   reallocated,   in   the   case   of   realloc).   If   memblock   is   NULL,   the   pointer   is   ignored   and   free   immediately   returns.   Attempting   to   free   an   invalid   pointer   (a   pointer   to   a   memory   block   that   was   not   allocated   by   calloc,   malloc,   or   realloc)   may   affect   subsequent   allocation   requests   and   cause   errors.  
  After   a   memory   block   has   been   freed,   _heapmin   minimizes   the   amount   of   free   memory   on   the   heap   by   coalescing   the   unused   regions   and   releasing   them   back   to   the   operating   system.   Freed   memory   that   is   not   released   to   the   operating   system   is   restored   to   the   free   pool   and   is   available   for   allocation   again.  
   
  When   the   application   is   linked   with   a   debug   version   of   the   C   run-time   libraries,   free   resolves   to   _free_dbg.   For   more   information   about   how   the   heap   is   managed   during   the   debugging   process,   see   The   CRT   Debug   Heap.  
  Top

15 楼xulecn(流氓兔)回复于 2006-12-03 19:19:10 得分 0

大家的意识是str的生命周期还没有结束,free(str)只是把它的空间释放了,但str   还存在。所以srt!=NULL为真。但是空间没有了,怎么还能strcyp()??Top

16 楼superxiaomm(小美)回复于 2006-12-04 16:02:00 得分 0

野指针的问题拉,就是你free和delete指针后,最好跟着这一句话p=NULL;Top

17 楼ppcat_001()回复于 2006-12-04 16:46:39 得分 0

free吧内存释放了,只是将当前的内存地址赋一段随机值,这个时候你还是可以用的(只要其他程序或线程不访问这里),但是很危险Top

18 楼superarhow(苏泊尔耗)回复于 2006-12-04 16:52:41 得分 0

-___-   难怪中兴的手机老是S机Top

19 楼elasma(人民警察人民踹 人民警察踹人民)回复于 2006-12-05 18:16:40 得分 0

我是老板,你是员工,我把你解雇了,但还让你管我的钱,很危险  
  就是这样Top

20 楼st311()回复于 2006-12-05 19:02:03 得分 0

[address]=str  
  [str]=*str  
   
  [xxx]表示地址为xxx的内存单元的内容,xxx标识了该内存单元。address这个内存单元在栈中(指针变量),str这个内存单元在堆中(malloc出来的)。free(str)后,堆回收了str这个内存单元,也就是说,从现在起,str这个单元可以由任何其他进程用malloc申请获得(而在free之前,str这个单元已经被该进程申请得到,别的进程是申请不到的),问题就出在这里,为什么free后还是可以输出"word"?这是因为str的值在free后没用任何改变(str的值是存储在address单元中的),执行输出语句的时候可能没有其他的进程申请str这个堆中的内存单元,也可能被别的进程申请了,但是并未改变单元里面的内容,因而正常的输出了“word”。然而,不能排除有别的进程在本进程free了str之后及输出str的内容之前申请到了str,同时修改了单元里面的内容,这时候的结果是未知的,不可预料的。轻则破坏数据,重则导致系统崩溃等。  
   
  因此这个题的结果就是随机结果,呵呵Top

21 楼missilery(导弹专家)回复于 2006-12-05 20:26:50 得分 0

C++builder打开codeguard工具运行,产生的.cgl文件内容:  
  Error   00025.   0x100430   (Thread   0x0E84):  
  Access   in   freed   memory:   Attempt   to   access   5   byte(s)   at   0x01216354.  
  strcpy(0x01216354,   0x004030AA   ["word"])Top

22 楼djdyq8566()回复于 2006-12-05 21:14:08 得分 0

我支持上面Shadow_Mor()的,他的是正确的Top

23 楼pxxx123()回复于 2006-12-05 21:37:59 得分 0

程序崩溃:   出现野指针.Top

相关问题

关键词

得分解答快速导航

  • 帖主:xulecn
  • besthyq
  • Shadow_Mor
  • loveliu0429
  • qqtcc
  • qqtcc

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo