Category Archives: Code

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;
}


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);
}