Archive for 01月, 2006

狗狗年马上到了

星期六, 01月 28th, 2006
给大家拜年啦.
bonne année!
je vous souhaite une excellente annee. Pleine de joies de toutes sortes, avec beaucoup de connaissances sympathiques.

TICPP-VOL2-习题代码-防御性编程

星期五, 01月 27th, 2006
//2-1
#include "../TestSuite/Test.h"
#include <vector>
using namespace std;
class VectorTest : public TestSuite::Test{
 vector<int> v_Int;
public:
 VectorTest(){
 }
 void run()
 {
  TestFuc();
 }
 void TestFuc(){
        v_Int.push_back(5);
  test_(v_Int.front()==5);
  test_(v_Int.back() == 5);
  test_(v_Int.at(0) == 5);
  v_Int.pop_back();
  test_(v_Int.size()==0);
  try{
   v_Int.at(1);
   fail_("invalited pos in vector \n");
  }
  catch (std::out_of_range) {

   succeed_();
  }
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 VectorTest obj_Test;
 obj_Test.run();
 obj_Test.report();
 getchar();
 return 0;
}

TICPP-VOL2-读书笔记-防御性编程

星期五, 01月 27th, 2006
生存理由:
1.提高代码的生存质量
2.XP(极限编程原则):边测试,边开发.
本章主要介绍了assert断言调试技术&TestSuite Framwork以及内存泄漏的宏调试方法.算是介绍了一些测试技术的基本思想&方法.概念不多.需要多读例子代码体会.
作者的TestSuite Framwork写的清晰易懂.在随后章节中大量应用.
一.断言
原则1.使用assert(case)对其中case的正确性进行判断.
二.简单单元测试框架
原则2.测试程序+编码 > 直接编码
原则3.阅读TestSuite的代码
三.调试技术
原则4.用于跟踪代码的宏
#define Trace(ARG) cout<<#ARG<<endl; ARG
#define D(A) cout<<#a<<"=["<<A<<"]"<<endl;
原则5.内存泄漏
1.数组边界监察
2.基类的非虚析构函数
3跟踪new/delete,malloc/free语句
文中介绍了一种重载new/delete,并且使用数组存储内存分配信息的方法.
本章的学习关键在于实践中体会XP编程思想.

张亚勤博士给科大学生的建议

星期三, 01月 25th, 2006

按:在网上看到张亚勤博士给科大学生的建议,觉得很有感触,ZZ过来,共勉之

第一点,选择自己的方向时,要有热情,不要为了短期的目标,不要为了表面的现象改变自己的兴趣follow your heart,不要随波逐流;

第二,要培养学习的能力而非只注重知识的积累(ability to learn instead of just knowledge itself)。 爱因斯坦说过,什么是最好的教育。 Education is what remains after one has forgotten what one has learned in school.(教育就是人忘记学校学的那些东西之后剩下的东西)。现在我已经忘了在学校里学过什么,重要的是我知道如何适应这种环境;

第三,开复一直很强调诚信,诚信非常重要。

总之,这是我给刚开始搞科研的本科生的建议:Follow your heart,有长远的眼光,敢于冒险,you are looking for a career instead of a job。这是我的导师给我讲的。

自助者人天助

星期三, 01月 25th, 2006

1.真正的自助者是令人敬佩的觉悟者,他会藐视困难,而困难在他的面前也会令人奇怪地轰然倒地—这个过程简直有如天神相助。

2.真正的自助者就像黑夜里发光的萤火虫,不仅会照亮自己,而且能赢得别人的欣赏—当人们欣赏一个人时,往往会用帮助的形式表示爱护—好运气因此而降临。

3.人们相信,一个真正的自助者最终会实现他的成功,而所有帮助过他的人也会为此感到欣慰。

4.如果自助者懂得报恩,人们就会给他更多的帮助,他因此可以更加轻松地面对生活。

TICPP-VOL2-习题代码-异常处理

星期三, 01月 25th, 2006
/////////////////////////////////////////////////////////
/////  VC2005                                               ////
////  Author:Tonyhuo                                    ////
////////////////////////////////////////////////////////
//1-2
#include <exception>
#include <iostream>
using namespace std;
class EXAMPLE{
public:
 class CharException{
 const char * str_Exception;
 public:
  CharException(const char * msg = 0):str_Exception(msg){}
  const char * What(){return str_Exception;}
 };
 void  f()
 {
  throw  CharException("MY Exception");
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 try{
        EXAMPLE obj;
  obj.f();
 }
 catch(EXAMPLE::CharException& e)
 {
  cout<<e.What()<<endl;
 }
 return 0;
}
///////////////////////////////////////////////////////////////////////////////////
1-4
#include <exception>
#include <iostream>
using namespace std;
int main()
{
 try{
 }
 catch(…)
 {
  cout<<"All exception caught!"<<endl;
 }
}
///////////////////////////////////////////////////////////////////////////////////////
1-9
//compile by g++
#include <exception>
#include <iostream>
#include <string>
using namespace std;
class MyException{};
class MyException2:public runtime_error{
public:
 MyException2(const string & msg =""):runtime_error(msg){}
};
void f(int i) throw(char,int,bool,MyException)
{  
 switch(i)
    {
    case 1: throw ‘a’;
    case 2: throw 32;
    case 3: throw true;
    case 4: throw MyException();
    case 5: throw MyException2();
    }
}
void my_unexpected()
{
 throw MyException2("test");;
}
void g() throw(MyException2){
    throw 3;
}
int main()
{
 for(int j =1;j<=4;j++)
  try { f(j);}
     catch(char)
  {
   cout<<"char exception caught"<<endl;
  }
  catch(int)
  {
   cout<<"int exception caught"<<endl;
  }    
  catch(bool)
  {
   cout<<"bool exception caught"<<endl;
  }    
  catch(MyException&)
  {
   cout<<"MyException exception caught"<<endl;
  }
    set_unexpected(my_unexpected);
 try{
  g();
 }
 catch (MyException2&)
 {
            cout <<"reback from g()"<<endl;
 }
 getchar();
 getchar();
}
///////////////////////////////////////////////////////////////////////////////////////////
1-11
#include <exception>
#include <iostream>
using namespace std;
class MotorError{};
class Motor{
public:
 Motor(){throw MotorError();}
};
class Car{
Motor obj_m;
public:
 Car():obj_m(){}
};
class Garage{
public:
 Car obj_c;
 Garage() try:obj_c(){

 }
 catch(MotorError&)
 {
  cout<<"Car::MotorError"<<endl;
  throw 1;
 }
};
int main()
{
 try
 {
  Garage obj;
 }
 catch(int)
 {
  cout<<"int exception caught"<<endl;
 }
 getchar();
}

TICPP-VOL2-读书笔记-异常处理

星期三, 01月 25th, 2006

生存理由:
1.异常处理远离异常发生地
2.针对对象的释放资源的管理
try
{
// ur code
throw ;//exception style
}
catch(type n)
{
  //handle exceptions of type
}
异常处理模型:中止&恢复模型.
一.异常匹配
原则1.对象或者指向派生类对象的引用都会与其基类处理器匹配.
原则2.捕获所有异常
         catch(…)
         {
         }
原则3.重新抛出异常,应用于调用链高层次异常处理.呵呵,就是高层处理底层异常.
        catch(…)
         {
         //ur code
         throw;
         }
原则4.不捕获异常.不是真正不捕获,是从底层到高层,寻找合适的异常处理器.如果没有,那么执

行,terminate()函数,其调用了abort()函数.
后果:全局对象&静态对象析构函数没有执行
set_terminate()函数制作自己的中止函数.
析构函数抛出异常几乎被禁止.
二.清理.就是资源释放.
原则5.
难点在于构造函数异常处理,处理方式:构造函数发生异常,析构函数不被调用.
第一种
1.构造函数捕获异常,用于资源释放
2.对象构造函数分配资源,析构函数释放资源.
原子性的处理.
第二种,用模板封装指针的方法.
    template<class T,int sz =1> class PWrap{
    T * Ptr;
    public:
   class RangeError();
   PWrap(){
   Ptr = new T[sz];
  }
  ~Pwrap(){
   delete []Ptr;
   }
   T& operator[](int i) throw(RangeError)
  {
    if(i>=0 && i<sz) return Ptr[i];
    throw RangeError();
  }

}
原则6.使用auto_ptr对象. 
class A{
public:
   void test(){cout<<"test"<<endl}:
}
int main()
{
    auto_ptr<A> pObject(new A());
    pObject->test(); //test is an object of auto_ptr
}
三.标准异常.
使用c++的标准异常可以大大提高工作效率.
四.异常规格说明.
example: void f() throw(toobig,toosmall,divzero) //函数捕获throw的三种异常
         void g() //捕获任何可能异常
         void h() throw() //不捕获任何异常
原则7.unexpected()&set_unexpected()的使用
example:
#include <exception>
#include <iostream>
using namespace std;
class up();
class fit();
void g();
void f(int i) throw(up,fit){
   switch(i)
 {
          case 1: throw up();
          case 2: throw fid();
        }
    g();
}

void g(){}; // version 1
void g(){throw 47} //version 2
void my_unexpected(){
   exit(0);
}
int main(){
   set_unexpected(my_unexpectd);
   for(int i=1;i<=3;i++)
      try{
 f(i); 
 }
      catch(up){
        cout<<"up caught"<<endl;
      }
 catch(fit)
      {
        cout<<"fit caught"<<endl;
      }
}

原则8.unexpected()抛出的异常类型不违反触发unexpected()的函数的异常规格说明的话,
函数恢复到函数被调用位置重新开始.
#include <exception>    // For std::bad_exception
#include <iostream>
#include <cstdio>
using namespace std;

// Exception classes:
class A {};
class B {};

// terminate() handler
void my_thandler() {
  cout << "terminate called" << endl;
  exit(0);
}

// unexpected() handlers
void my_uhandler1() { throw A(); }
void my_uhandler2() { throw; }

// If we embed this throw statement in f or g,
// the compiler detects the violation and reports
// an error, so we put it in its own function.
void t() { throw B(); }

void f() throw(A) { t(); }
void g() throw(A, bad_exception) { t(); }

int main() {
  set_terminate(my_thandler);
  set_unexpected(my_uhandler1);
  try {
    f();
  } catch(A&) {
    cout << "caught an A from f" << endl;
  }
  set_unexpected(my_uhandler2);
  try {
    g();
  } catch(bad_exception&) {
    cout << "caught a bad_exception from g" << endl;
  }
  try {
    f();
  } catch(…) {
    cout << "This will never print" << endl;
  }
} ///:~

原则9.如果函数规格说明包括bad_exception,则unexpected()函数抛出异常替换为bad_expection.
原则10.如果函数规格说明不包括bad_exception,则调用terminated()函数.
原则11.派生类跟基类要遵循is-a的关系.
#include <exception>    // For std::bad_exception
#include <iostream>
#include <cstdio>
using namespace std;

// Exception classes:
class A {};
class B {};

// terminate() handler
void my_thandler() {
  cout << "terminate called" << endl;
  exit(0);
}

// unexpected() handlers
void my_uhandler1() { throw A(); }
void my_uhandler2() { throw; }

// If we embed this throw statement in f or g,
// the compiler detects the violation and reports
// an error, so we put it in its own function.
void t() { throw B(); }

void f() throw(A) { t(); }
void g() throw(A, bad_exception) { t(); }

int main() {
  set_terminate(my_thandler);
  set_unexpected(my_uhandler1);
  try {
    f();
  } catch(A&) {
    cout << "caught an A from f" << endl;
  }
  set_unexpected(my_uhandler2);
  try {
    g();
  } catch(bad_exception&) {
    cout << "caught a bad_exception from g" << endl;
  }
  try {
    f();
  } catch(…) {
    cout << "This will never print" << endl;
  }
} ///:~
原则12.异常规格说明主要为非模板类实现.
五.异常安全.
原则13.使用clone技术实现新增对象的安全分配内存&初始化.
六.异常使用原则.
原则14.避免使用异常
1.异步事件.2.处理简单错误.3.程序流程控制.4.强迫使用5.新异常,老代码.
原则15.使用异常.
1.使用异常规格说明时,要编写自己的unexpected函数.
2.尽量使用标注异常.
3.嵌套用户自己的异常.
4.使用异常层次结构.捕获基类型异常.
5.多重继承.
6.引用捕获异常.
7.构造函数中抛出异常.
8.不在析构函数抛出异常.
9.避免悬挂指针.auto_ptr

聊天

星期日, 01月 22nd, 2006
前天公司聚会,几杯啤酒下肚,感慨就出来了.跟老韩聊了一个多小时的cpp,嘎嘎,韩哥真是勇猛过人,对软件业的发展&本身语言的体会到了云里雾里的感觉.
跟老韩多学几把手艺,回头哥们有空也写个体会文章啥.呵呵
努力把.

看金刚^^^

星期二, 01月 17th, 2006
这部片子在机器里面放了足足有十天了,一直说看,找不到大段的时间来好好体会这部三个小时的片子.:(
周六+周日的接力竞赛终于看完了这部感人肺腑的片子.
不说偏执的导演&女主角的美丽.金刚这部片子本身作为科幻题材的影片事实上在不少细节并处理的不是太好.例如,岛上土著居民的突然消失.
但是这些并不影响它感动你,这个丑陋的,甚至有时候是凶残的(自然界的胜者生存)的黑猩猩,它一开始并非为了女主角的美丽所蛰伏! 而动物所与生具来的那种占有欲,猎物被别人抢走那种不由自主的去抢夺保护了女主角.
人类的贪婪&生物本身的占有欲望想比变得更为可怕,贪婪带来了灾难.
也让原本舒适生活在骷髅岛的金刚失去了生存的意义.
最后的十五分钟,让人觉的窒息.

笑无极-一个馒头引发的血案

星期二, 01月 17th, 2006

http://218.22.209.13/bbsgy/wj.wmv