Monthly Archives: September 2005

Test for “sizeof” operator

/*
  Name: Test for "sizeof" operator
  Copyright: no
  Author: David Vandevoorde's solution to "the C++ Programming Language"
  Typer:  Tom Gee
  Date: 01-03-03 22:00
  Description: this program shows how many bits the compiler allocates
               for each data type.
*/

#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;

struct Polymorph
{
    virtual ~Polymorph(){}
};

enum Bit{ Zero, One };
enum Intensity{ black = 0, brightness = 1000};

int main(int argc, char *argv[])
{
    //Integers
    cout<<"//Integers"<<endl;
    cout<<"sizeof(bool)="<<sizeof(bool)<<endl;
    cout<<"sizeof(char)="<<sizeof(char)<<endl;
    cout<<"sizeof(signed char)="<<sizeof(signed char)<<endl;
    cout<<"sizeof(unsigned char="<<sizeof(unsigned char)<<endl;
    cout<<"sizeof(wchar_t)="<<sizeof(wchar_t)<<endl;
    cout<<"sizeof(signed short=)"<<sizeof(signed short)<<endl;
    cout<<"sizeof(unsigned short)="<<sizeof(unsigned short)<<endl;
    cout<<"sizeof(signed int)="<<sizeof(signed int)<<endl;
    cout<<"sizeof(unsigned int)="<<sizeof(unsigned int)<<endl;
    cout<<"sizeof(signed long)="<<sizeof(signed long)<<endl;
    cout<<"sizeof(unsigned long)="<<sizeof(unsigned long)<<endl;
    #ifdef LONGLONG_EXT
    cout<<"sizeof(long long)="<<sizeof(long long)<<endl;
    #endif
    
    //floats
    cout<<"//floats"<<endl;    
    cout<<"sizeof(float)="<<sizeof(float)<<endl;
    cout<<"sizeof(double)="<<sizeof(double)<<endl;
    cout<<"sizeof(long double)="<<sizeof(long double)<<endl;
    
    //pointers
    cout<<"//poiters"<<endl;
    cout<<"sizeof(int *)="<<sizeof(int*)<<endl;
    cout<<"sizeof(int (*)())="<<sizeof(int(*)())<<endl;    
    cout<<"sizeof(void (Polymorph::*)())="<<sizeof(void (Polymorph::*)())<<endl; 
    cout<<"sizeof(void*)="<<sizeof(void*)<<endl;
    cout<<"sizeof(Bit)="<<sizeof(Bit)<<endl;
    cout<<"sizeof(Intensity)="<<sizeof(Intensity)<<endl;          
    
  system("PAUSE");	
  return 0;
}


Advertisements

Test if STLport is correctly ported to the compiler

/*
  Name: Test for STLport
  Copyright: no
  Author: Meng Yan
  Typer: Tom Gee
  Date: 01-03-03 22:15
  Description: Test if STLport is correctly ported to the compiler
*/

#include <iostream>
#include <rope>

using namespace std;

int main()
{
    crope bigstr1("It took me about oen hour more ");
    crope bigstr2("to plug the STLport into Visual C++");
    crope story = bigstr1 + bigstr2;
    cout << story<<endl;
    return 0;  
}


Matlab C/C++ compiler Configration test code

Test if Microsoft Visual Studio is correctly configed to use Matlab C++ functions.
 
/*
  Name: Test for Matlab C++ Mathematics Functions
  Copyright: no
  Author:  <<MATLAB外部程序接口(6.X)>>
  Typer:  Tom Gee
  Date:   01-03-03 22:08
  Description: Test if Microsoft Visual Studio is correctly configed to use
               Matlab C++ functions. 
*/

#include "matlab.hpp"
#include <ostream>

using namespace std;

static double data0[] = {2, 4, 6, 8};
static double data1[] = {1, 5, 3, 7};

int main(void)
{
    // standard C++ solution.
    int i =0;
    double data2[4];
    for(i = 0; i < 4; ++i)
    {
        data2[i] = (data1[i] + data0[i]) / 2.0;
        cout<< data0[i] << " + " << data1[i] << " = " << data2[i] << endl;
    }
    
    //Matlab solution.
    mwArray mat0(2, 2, data0);
    mwArray mat1(2, 2, data1);
    mwArray mat2;
    
    mat2 = rdivide(plus(mat0, mat1), 2);
    cout<< mat0 << "\t + \n" << mat1 <<"\t/2 = \n" << mat2;
    return(EXIT_SUCCESS);
}
 


A class wrapping all kinds of timing under windows

This is a piece of old code written 2 years ago.
 
It invokes system function to implement pretty precision time measuring.
/********************************************************
 * Name:        CtTimer.h                               *
 * Copyright:   all rights reserved.                    *
 * Author:      tomgee,                                 *
 * Date:        21-09-03 01:34                          *
 * Description: a class wrapping all kinds of timing    *
 *              under windows.                          *
 ********************************************************
 * link KERNEl32.lib for QueryPerformanceCounter()      *
 * link winmm.lib for timeGetTime()                     *
 * Version:     1.0 2003-10-06                          *
 ********************************************************/


#ifndef CTTIMER_H
#define CTTIMER_H

#include <ctime>          // for clock()
#include <Windows.h>      // for timeGetTime() and QueryPerformanceCounter()

class CtTimer
{
public:
    CtTimer();
    ~CtTimer(){};
    
    enum{
        FN_CLOCK = 0,       // use function clock() .
        FN_TIMEGETTIME,     // use function timeGetTime().
        FN_QP,              // use function QueryPerformance();
        
        FN_MAX
        }; 
private:
    LARGE_INTEGER m_qstart, m_qend;
    LARGE_INTEGER m_perffreq;
    long m_start, m_end;
    short m_fn;
public:
    void SetFunction(short fn);
    void Start();
    void End();
    double Elapse();
    double Test();
        
};

CtTimer::CtTimer()
{
    QueryPerformanceFrequency(&m_perffreq);
    m_fn = FN_CLOCK;
}

void CtTimer::SetFunction(short fn)
{
    if (fn<FN_MAX) m_fn = fn;
}

void CtTimer::Start()
{   
    switch(m_fn)
    {
        case FN_CLOCK:        m_start =clock(); break;
        case FN_TIMEGETTIME:   m_start = timeGetTime(); break;
        case FN_QP:           QueryPerformanceCounter(&m_qstart);break;
        default:              ;  
    }

}

void CtTimer::End()
{
    switch(m_fn)
    {
        case FN_CLOCK:        m_end =clock(); break;
        case FN_TIMEGETTIME:   m_end = timeGetTime(); break;
        case FN_QP:           QueryPerformanceCounter(&m_qend);break;
        default:              ;  
    }
}

double CtTimer::Elapse()
{
    if (FN_QP == m_fn)
        return(((double)(m_qend.QuadPart- m_qstart.QuadPart)/(double)m_perffreq.QuadPart));
    else
        return (double)(m_end - m_start);    
}

double CtTimer::Test()
{
    Start();
    Sleep(500);
    End();
    return Elapse();
}

#endif


A Literal Converter for Integers

Introduction

Electronic engineers often feel the need to plot experiment data observed from electronic instruments (e.g. Oscilloscope meters). More often than not, the data is stored in one format (e.g. hexadecimal), while the plotting software needs another input format (e.g. decimal). This is quite inconvenient. I finally decided to wrap the conversion into a C++ class: CLiteralConverter. In practice, integer literals usually come in four guises: decimal, octal, hexadecimal, and binary literals. So CLiteralConverter only provides conversion between these four formats.

 

Although demonstrated with a MFC demo application, The code is written in standard C++. So it can be used with different platforms easily.
 

 

C++ Constructor & Destructor

The following piece of code shows the birth and death of an object

under various circumstances: when the constructor and destructor are called.

 

#include <iostream>

using std::cout;

using std::endl;

 

class CScopeTest

{

private:

      static int count;

      int index;

public:

      CScopeTest() {index = count++; cout<<"ctor"<<index<<endl;}     

      ~CScopeTest() {cout<<"dtor"<<index<<endl;}           

     

};

int CScopeTest::count = 1;

 

void f()

{

    CScopeTest t3;

}

 

CScopeTest t1;

 

int main()

{

    cout<<">> main()\n";

    CScopeTest t2;

    static CScopeTest t4;

    f();

    cout<<"<< main()\n";

    return 0;

}

 

Result:

ctor1
>> main()
ctor2
ctor3
ctor4
dtor4
<< main()
dtor2
dtor3
dtor1