我辈岂是蓬蒿人!

C++ && keyWordSpotting

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  11 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

2006年8月29日 #

 

 1 #ifndef _GHH_TIMER_GHH_
 2 #define _GHH_TIMER_GHH_    1
 3 
 4 // File: ghhTimer.h
 5 // Date: 2006.08.14
 7 
 8 #include <ctime>
 9 
10 // 类导出导入类别的符号定义
11 #ifdef _DLL_FILE_
12 #define PORTTYE __declspec(dllexport) // 导出
13 #else
14 #define PORTTYE __declspec(dllimport) // 导入
15 #endif    // end of _DLL_FILE_
16 
17 /****************************************************************************
18  * 类名称    ghhTimer
19  * 
20  * 描述
21  *    本类对标准库计时函数进行了封装,可以实现非常精确的计时,毫秒级别
22  *
23  * 使用说明
24  *    在所要计时程序段之前,调用Start函数,程序段结束时,调用Pause函数,
25  *    多次调用程序段,即可以比较精确的估计程序段的运行时间
26 ****************************************************************************/
27 class  PORTTYE ghhTimer
28 {
29 public:
30     ghhTimer();
31 
32 public:
33     bool Start(void); 
34     bool Stop(void);    
35     bool Pause(void);
36     size_t GetSeconds(voidconst;
37     size_t GetMiliSeconds(voidconst;
38 
39 private:
40     enum {run = 1, stop, pause} _Status;
41     time_t _Clock;
42     time_t _TotalClocks;
43 };
44 
45 #endif // end of _GHH_TIMER_GHH_

  1 #ifndef _DLL_FILE_
  2 #define _DLL_FILE_
  3 #endif
  4 #include "ghhTimer.h"
  5 
  6 /****************************************************************************
  7  * about the important function "clock()"
  8  * #include <time.h>
  9  * clock_t clock( void );
 10  * The clock() function returns the processor time since the program started, 
 11  * or -1 if that information is unavailable. 
 12  * To convert the return value to seconds, divide it by CLOCKS_PER_SEC. 
 13  * (Note: if your compiler is POSIX compliant, 
 14  * then CLOCKS_PER_SEC is always defined as 1000000.)
 15  ***************************************************************************/
 16 
 17 
 18 // 构造函数,设置初始状态
 19 ghhTimer::ghhTimer() : _Status(stop), _Clock(0), _TotalClocks(0)
 20 {
 21 }
 22 
 23 // 当表已经停止或者暂停时启动停表,成功返回true,否则返回false
 24 bool ghhTimer::Start(void)
 25 {
 26     switch (_Status)
 27     {
 28     case stop :
 29         _TotalClocks = 0;
 30         _Clock = clock();
 31         break;
 32 
 33     case pause :
 34         _Clock = clock();
 35         break;
 36 
 37     case run :
 38         break;
 39 
 40     default :
 41         return false;
 42     }
 43 
 44     _Status = run;
 45 
 46     return true;
 47 }
 48 
 49 // 表运行时暂停计时,成功返回true,否则返回false
 50 bool ghhTimer::Pause(void)
 51 {
 52     switch (_Status)
 53     {
 54     case stop :
 55     case pause :
 56         break;
 57 
 58     case run :
 59         _TotalClocks += (clock() - _Clock);
 60         _Clock = 0;
 61         _Status = pause;
 62         break;
 63 
 64     default :
 65         return false;
 66     }
 67 
 68     return true;
 69 }
 70 
 71 // 表运行或暂停时停止计时
 72 bool ghhTimer::Stop(void)
 73 {
 74     switch (_Status)
 75     {
 76     case stop :
 77     case pause :
 78         break;
 79         
 80     case run :
 81         _TotalClocks +=(clock() - _Clock);
 82         _Clock = 0;
 83         break;
 84 
 85     default :
 86         return false;
 87     }
 88 
 89     _Status = stop;
 90     
 91     return true;
 92 }
 93 
 94 // 得到当前积累的秒数
 95 size_t ghhTimer::GetSeconds(voidconst
 96 {
 97     time_t Clocks;
 98 
 99     switch (_Status)
100     {
101     case stop:
102     case pause:
103         Clocks = _TotalClocks;
104         break;
105 
106     case run:
107         Clocks = _TotalClocks + clock() - _Clock;
108         break;
109 
110     default:
111         return false;
112     }
113     return (Clocks / CLOCKS_PER_SEC);
114 }
115 
116 // 得到当前积累的毫秒数
117 size_t ghhTimer::GetMiliSeconds(voidconst
118 {
119     time_t Clocks;
120 
121     switch(_Status)
122     {
123     case stop:
124     case pause:
125         Clocks = _TotalClocks;
126         break;
127     case run:
128         Clocks = _TotalClocks + clock() - _Clock;
129         break;
130 
131     default:
132         return false;
133     }
134     return (Clocks * 1000 / CLOCKS_PER_SEC);
135 }
136 
posted @ 2006-08-29 09:51 keyws 阅读(321) | 评论 (0)编辑 收藏

2006年8月22日 #

Shifting from C to C++

1. To C++ programmer, for example, a pointer to a pointer looks a little funny. Why, we wonder, wasn’t a reference to a pointer used  instead?

       const char chr[] = "chenzhenshi&guohonghua";

       const char*  pchr = chr;

       const char** ppchr = &pchr;

       const char*&  rpchr  = pchr; // a reference to a pointer

       std::cout << pchr << ' ' << *ppchr << ' ' << rpchr << std::endl;

 

2. C is a fairly simple language. All it really offers is macros, pointers, structs, arrays, and functions. No matter what the problem is, the solution will always boil down to macros, pointers, structs, arrays, and functions. Not so in C++. The macros, pointers, structs, arrays and functions are still there, of course, but so are private and protected members, function overloading, default parameters, constructors and destructors, user-defined operators, inline functions, references, friends, templates, exceptions, namespaces, and more. The design space is much richer in C++ than it is in C: there are just a lot more options to consider.

Item 1: Prefer const and inline to #define

3. The Item might better be called “prefer the compiler to the preprocessor”.

4.    const char* pc;

       pc = a1;

       std::cout << pc << std::endl;

       pc = a2;

       std::cout << pc << std::endl;

 

       const char* const pcc = "a const pointer to a const char array";

       std::cout << pcc << std::endl;

       // error C2166: l-value specifies const object

       // pcc = a1;  // error!

       std::cout << pcc << std::endl;

5. You can define a const variable in a class, but it must be static const, and have a definition in an implementation file.

// .h file

class try_const

{

public:

       static const int num;

};

// .cxx file

const int try_const::num = 250;

6. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using an inline function.

Template <class type>

Inline const type& max (const type& a, const type& b)

{

Return a > b ? a : b ;

}

7. Given the availability of consts and inlines, your need for the preprocessor is reduced, but it's not completely eliminated. The day is far from near when you can abandon #include, and #ifdef/#ifndef continue to play important roles in controlling compilation. It's not yet time to retire the preprocessor, but you should definitely plan to start giving it longer and more frequent vacations.

Item 2: Prefer <iostream> to <stdio.h>

8.  scanf and printf are not type-safe and extensible.

9.  In particular, if you #include <iostream>, you get the elements of the iostream library ensconced within the namespace std (see Item 28), but if you #include <iostream.h>, you get those same elements at global scope. Getting them at global scope can lead to name conflicts, precisely the kinds of name conflicts the use of namespaces is designed to prevent.

Item 3: Prefer new and delete to malloc and free

10. The problem with malloc and free(and their variants) is simple : they don’t know about constructors and destructors.

11. free 操作不会调用析构函数,如果指针所指对象本身又分配了内存,则会造成内存丢失。

Item 4: Prefer C++ style comments

Memory Management

12. Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently.

Item 5: Use the same form in corresponding uses of new and delete

13. When you use new, two things happen. First, memory is allocated. Second, one or more constructors are called for that memory. When you use delete, two other things happen: one or more destructors are called for the memory, then the memory is deallocated.

14. The standard C++ library includes string and vector templates that reduce the need for built-in arrays to nearly zero.

Item 6: Use delete on pointer members in destructors

15. Speaking of smart pointers, one way to avoid the need to delete pointer members is to replace those members with smart pointer objects like the standard C++ Library’s auto_ptr.

Item 7: Be prepared for out-of-memory conditions

Item 8: Adhere to convention when writing operator new and operator delete

Item 9: Avoid hiding the “normal” form of new

Item 10: Write operator delete if you write operator new

让我们回过头去看看这样一个基本问题:为什么有必要写自己的 operator new operator delete ?答案通常是:为了效率。缺省的 operator new operator delete 具有非常好的通用性,它的这种灵活性也使得在某些特定的场合下,可以进一步改善它的性能。尤其在那些需要动态分配大量的但很小的对象的应用程序里,情况更是如此。

 

posted @ 2006-08-22 17:24 keyws 阅读(94) | 评论 (0)编辑 收藏

  
   动态分配的对象:程序员完全控制分配与释放,分配在程序的空闲存储区(free store)的可用内存池中。
 
 1
)单个对象的动态分配与释放;
 new
表达式没有返回实际分配的对象,而是返回指向该对象的指针。对该对象的全部操作都要通过这个指针间接完成。
 
随机分配的内存具有随机的位模式,建议初始化,例如:

 int* pi = new int(0);


 

空闲存储区是有限的资源,若被耗尽,new表达式会失败,抛出bad_alloc异常。
 
这样做没有必要:

     if  ( pi != 0 )
        delete pi;

  说明:如果指针操作数被设置为0,则C++保证delete表达式不会调用操作符delete()。所以没有必要测试其是否为0
 
delete表达式之后,pi被称作空悬指针,即指向无效内存的指针。空悬指针是程序错误的根源,建议对象释放后,将该指针设置为0
 
 2
auto_ptr
 auto_ptr
C++标准库提供的类模板,它可以帮助程序员自动管理用new表达式动态分配的单个对象,但是,它没有对数组管理提供类似支持。它的头文件为:
 

    #include  < memory >  

  auto_ptr对象的生命期结束时,动态分配的对象被自动释放。
 auto_ptr
类模板背后的主要动机是支持与普通指针类型相同的语法,但是为auto_ptr对象所指对象的释放提供自动管理。例:

     //  第一种初始化形式
    std::auto_ptr< int > pi(  new   int (1024) );     // 

 auto_ptr 类模板支持所有权概念,当一个auto_ptr对象被用另一个auto_ptr对象初始化赋值时,左边被赋值或初始化的对象就拥有了空闲存储区内底层对象的所有权,而右边的auto_ptr对象则撤消所有责任。例:

    std::auto_ptr<std:: string > pstr_auto(  new  std:: string ( "Brontonsaurus" ) );
    
// 
第二种初始化形式
    std::auto_ptr<std:: string > pstr_auto2( pstr_auto );

  判断是否指向一个对象,例:

     //  第三种初始化形式
    auto_ptr < int >  p_auto_int;    
    
if  ( p_auto_int. get ()  ==   0  )
        
    
else
        
//  重置底层指针,必须使用此函数        
        p_auto_int.reset(  new   int 1024  ) );


 3)数组的动态分配与释放
 
建议使用C++标准库string,避免使用C风格字符串数组。
 
为避免动态分配数组的内存管理带来的问题,一般建议使用标准库vectorliststring容器类型。
 
 4
)常量对象的动态分配与释放
 
可以使用new表达式在空闲存储区内创建一个const对象,例:

     //  此时必须初始化,否则编译错误
     const   int * pci =  new   const   int (1024);    

  我们不能在空闲存储区创建内置类型元素的const数组,原因是:我们不能初始化用new表达式创建的内置类型数组的元素。例:

     const   int * pci =  new   const   int [100];  //  编译错误


 5
)定位new表达式
 new
表达式的第三种形式允许程序员要求将对象创建在已经被分配好的内存中。称为:定位new表达式(placement new expression)。程序员在new表达式中指定待创建对象所在的内存地址。如下所示:
 new
place_address) type-specifier
 
注意:place_address必须是个指针,必须包含头文件<new>。这项设施允许程序员预分配大量的内存,供以后通过这种形式的new表达式创建对象。例如:

    #include  < iostream >
    #include 
< new >      //  必须包含这个头文件
    
    
const   int  chunk  =   16 ;
    
class  Foo
    
{
        
    }
;
    
    
char *  buf  =   new   char sizeof (Foo)  *  chunk ];
    
    
int  main( int  argc,  char *  argv[])
    
{
        
//  只有这种形式的创建,没有配对形式的delete 
        Foo *  pb  =   new  (buf) Foo;
                
        delete[] buff;
        
        
return   0 ;
    }

posted @ 2006-08-22 14:54 keyws 阅读(218) | 评论 (2)编辑 收藏

2006年8月20日 #

Introduction

1. A declaration tells compilers about the name and type of an object, function, class, or template, but it omits certain details.

2. A definition, on the other hand, provides compilers with the details. For an object, the definition is where compilers allocate memory for the object. For a function or a function template, the definition provides the code body. For a class or a class template, the definition lists the members of the class or template.

3. When you define a class, you generally need a default constructor if you want to define arrays of objects.Incidentally, if you want to create an array of objects for which there is no default constructor, the usual ploy is to define an array of pointers instead. Then you can initialize each pointer separately by using new.

4. Probably the most important use of the copy constructor is to define what it means to pass and return objects by value.

5. From a purely operational point of view, the difference between initialization and assignment is that the former is performed by a constructor while the latter is performed by operator=. In other words, the two processes correspond to different function calls. The reason for the distinction is that the two kinds of functions must worry about different things. Constructors usually have to check their arguments for validity, whereas most assignment operators can take it for granted that their argument is legitimate (because it has already been constructed). On the other hand, the target of an assignment, unlike an object undergoing construction, may already have resources allocated to it. These resources typically must be released before the new resources can be assigned. Frequently, one of these resources is memory. Before an assignment operator can allocate memory for a new value, it must first deallocate the memory that was allocated for the old value.

//  a possible String constructor
String::String( const   char   * value)
{
    
if  (value)
    

        
//  if value ptr isn't null
        data  =   new   char [strlen(value)  +   1 ];
        strcpy(data,value);
    }
    
    
else  
    

        
//  handle null value ptr3
        data  =   new   char [ 1 ];
        
* data  =   ' \0 ' //  add trailing
         null   char
    }

}


//  a possible String assignment operator

String
&  String:: operator = ( const  String &  rhs)
{
    
if  ( this   ==   & rhs)
        
return   * this //  see Item 17

    delete [] data; 
//  delete old memory
    
    data 
=   //  allocate new memory
         new   char [strlen(rhs.data)  +   1 ];

    strcpy(data, rhs.data);
    
    
return   * this //  see Item 15
}


6. These different casting forms serve different purposes:

const_cast is designed to cast away the constness of objects and pointers, a topic I examine in Item 21.

dynamic_cast is used to perform "safe downcasting," a subject we'll explore in Item 39.

reinterpret_cast is engineered for casts that yield implementation-dependent results, e.g., casting between function pointer types. (You're not likely to need reinterpret_cast very often. I don't use it at all in this book.)

static_cast is sort of the catch-all cast. It's what you use when none of the other casts is appropriate. It's the closest in meaning to the conventional C-style casts.

posted @ 2006-08-20 16:10 keyws 阅读(68) | 评论 (0)编辑 收藏

2006年8月17日 #

     摘要: Thinking in C++, 2nd ed. Volume 1 ©2000 by Bruce Eckel Unary operators The following example shows the syntax to overload all the unary operators, ...  阅读全文
posted @ 2006-08-17 12:14 keyws 阅读(75) | 评论 (0)编辑 收藏

2006年8月16日 #

   学习数据结构,用到了输入输出运算符重载,结果编译之下,错误重重,,随即停止学习进度,追查祸源,在花费巨大脑力与时间成本之后,终于知自己错误之所在,定位于名字空间之困扰。为牢记教训,写一简化版本记录 debug 过程如下,警示自己。

问题
   

   有如下两个代码文件:
   // 20060816_operator.cxx

 1 #include <iostream>
 2 using namespace std;
 3 #include "20060816_operator.h"
 4 int main(int argc, char* argv[])
 5 {
 6     const std::string GHH("GuoHonghua");
 7     Honghua ghh(GHH);
 8     cout << ghh << endl;
 9     return 0;
10 }
   

// 20060816_operator.h

 1 #ifndef _20060816_OPERATOR_GHH_
 2 #define _20060816_OPERATOR_GHH_    1
 3 
 4 // Author : GuoHonghua
 5 // Date : 2006.08.16
 6 // File : 20060816_operator.h
 7 
 8 #include <iostream>
 9 #include <string>
10 class Honghua
11 {
12     friend std::ostream& operator<<(std::ostream& os, const Honghua& ghh);
13 public:
14     Honghua(const std::string& ghh) : _ghh(ghh)
15     {
16     }
17 
18 private:
19     std::string _ghh;
20 };
21 
22 std::ostream& operator<<(std::ostream& os, const Honghua& ghh)
23 {
24     os << ghh._ghh;
25     return os;
26 }
27 
28 #endif // end of _20060816_OPERATOR_GHH_

用上面两个文件建立工程,vc6.0下编译会出错。报告如下:

--------------------Configuration: 20060816_operator - Win32 Debug--------------------

Compiling...

20060816_operator.cxx

f:\ghh_project\cxxdetail\20060816_operator.h(24) : error C2248: '_ghh' : cannot access private member declared in class 'Honghua'

        f:\ghh_project\cxxdetail\20060816_operator.h(19) : see declaration of '_ghh'

F:\ghh_project\CxxDetail\20060816_operator.cxx(8) : error C2593: 'operator <<' is ambiguous

Error executing cl.exe.

 

20060816_operator.exe - 2 error(s), 0 warning(s)
解决:
   

1.  调整名字空间声明和自定义头文件的次序,如下:

1 #include <iostream>
2 #include "20060816_operator.h"
3 using namespace std;
4 int main(int argc, char* argv[])
5 

2.  把类定义于标准名字之内,20060816_operator.h文件修改如下(不推荐)

 1 #ifndef _20060816_OPERATOR_GHH_
 2 #define _20060816_OPERATOR_GHH_    1
 3 
 4 // Author : GuoHonghua
 5 // Date : 2006.08.16
 6 // File : 20060816_operator.h
 7 
 8 #include <iostream>
 9 #include <string>
10 namespace std
11 {
12     class Honghua
13     {
14         friend std::ostream& operator<<(std::ostream& os, const Honghua& ghh);
15     public:
16         Honghua(const std::string& ghh) : _ghh(ghh)
17         {
18         }
19         
20     private:
21         std::string _ghh;
22     };
23     
24     
25     std::ostream& operator<<(std: