免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2457 | 回复: 0
打印 上一主题 下一主题

[C++] 华为面试题 -- C++ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-03-24 10:16 |只看该作者 |倒序浏览
这是我从别的地方转过来的!

这是一道中级题. 高级题其实还不如这道题                                                                                                              

1) 指出以下示意代码的错误之处                                                        
                                                      
  1. CRITICAL_SECTION g_CriticalSection=NULL;
  2. char *g_string=NULL;
  3.                                                                                                                
  4. class MyClass
  5. {
  6. public:
  7.         int InitInstance();
  8.         int threadA();
  9. };
  10.                                                                                                                
  11. int MyClass::Init()
  12. {
  13.         InitCriticalSection(&g_CriticalSection);
  14.         CreateThread(threadA,...);
  15. }
  16.                                                                                                                
  17. int MyClass::threadA()
  18. {
  19.         EnterCriticalSection(g_CriticalSection)
  20.         if(g_string!=NULL)
  21.         {
  22.                 delete g_string;
  23.         }
  24.         g_string = new char[1024];
  25.         ...
  26.         delete g_string;
  27.         g_string = NULL;
  28.         ExitCriticalSection(g_CriticalSection)
  29.         return 0;
  30. }
复制代码


2) 如果MyClass改成如下定义,请写出实现上面功能的代码
                                                                                                               
  1. class MyClass
  2. {
  3.         static const CRITICAL_SECTION m_CriticalSection;
  4. public:
  5.         MyClass() { EnterCriticalSection(m_CriticalSection); }
  6.         ~MyClass(){ ExitCriticalSection(m_CriticalSection); }
  7. };
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP