Posts

Showing posts from 2006

C/C++ Basic - protect data and pointer with const

char * const lpszct = "test"; //protecting pointer
char szBuff[10];
strcpy(szBuff, "123");
const char* lpctsz=szBuff; //protecting data pointed.

lpszct = "12"; //<-- compile error trying to change the pointer address

lpszct ++; //<-- compile error trying to change the pointer address

lpctsz[0] = '7'; //<-- compile error where trying to change the data

Google Search Cheat Sheet

define:[word] Search word in dictionaries
filetype:[ext] Query result with filetyp. E.g. "C++ filetype:pdf"
/[country] Limited search to coutry E.g. "food /malaysia
/language:[language shorthand] Limited to particular language group. e.g. "cari /language:ms"
site:[domain] Limited to a domain. e.g. "thread site:edu"
inanchor:[url] Limited search to anchor <a>
inurl:[word] Limited search to URL of sites
intitle:[word] Limited search to web site title
(content inside html title tag i think)
intext:[word] Limited search to web site content

Kill all my processes in current Unix machine (shell script)

This is a korn shell script, but should work in other shell with little change or none.

#created by Anthony Yio, 28 Dec 06
#!/bin/ksh
MYUSER=`whoami`
for MYPID in `ps -ef | grep $MYUSER | awk '{ print $(2) }' | sort -r`
do
`kill -9 $MYPID`
done

A little perl script to spice up your unix shell startup

Content of perl script
-----------
use strict;

$/ = "\n%%%\n"; #define separator for array
open ARTS, "art.dat" or die $!;
my @file = <arts>;
my $random = rand(@file);

my $ascii_art = $file[$random];
$ascii_art=~s/\n%%%\n//; #remove the separator string
print $ascii_art, "\n";
----------

sample content of art.dat, you can get more from the mentioned web site or newsgroup
(sample below taken from http://www.chris.com/ASCII/)
----------

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$*" *$$$$$$$$$$$$$$$$$$$$$$$$$$$" "*" *$$$$$$" ^$$$"$$$$$$$$$$$
$$$$$$$$$" z$P"$$$$$$$$$$$$$$$$$$$$$$$$P e$"e4$b. "$$" d$ $P $$$$$$$$$$$$
$$$$$$$" .$$$ J$$$$$$$$$$$$$$$$$$$$$$$" .$Pz$$4$$$$c z" $$*" z$$$$$$$$$$$$$
$$$$$$ 4$$$$$$$$$$$$$$$$$$$$$$$$$$$P z$$4$$%$$$$$$$d " z$$$$$$$$$$$$$$$
$…

CTags and VIM

Sample generate tags file command
ctags -f {tag file location} --recurse --langmap=C++:.C.h.c.cpp.hpp --languages=C++ --extra=+q --fields=+i --verbose {source parent folder e.g. /usr/my_source/}
After tag file generated, addset tags={tag file location 1}, {tag file location 2} to .vimrc
in VIM
type
:set tags={tag file location 1}, {tag file location 2} or add it to .vimrc in VIM


For ultraedit, the ctag parameter is
-L %fi -f %fo --language-force=C++ --extra=+q --fields=+i



If using VIM as editor, the supported commands are

:ts - tag, class definition as priority
:tj - tag (can use /pattern or /^pattern. Since it is pattern search, it is not binary search mode which would be slow)
:po or ctrl + 't' - older tag search stack
ctrl ']' on selected text to call tag search directly.
: or to scroll for previous calls

(refer http://www.vim.org/htmldoc/tagsrch.html )

VIM commands which is useful when use in conjunction with ctags
shift + * - start to search this word in the document
shift…

Operator overload

A a;

if(a) <--- this will trigger operator bool() and also operator void const*() const{
{
}


}

boost style
(typedef T * (this_type::*unspecified_bool_type)() const;operator unspecified_bool_type() const // never throws{return px == 0? 0: &this_type::get;})

if (a == "ss") <-- a combinations of few operator overload.

Foo(std::string const& s);
Foo(std::string s);

bool operator==(Foo const& lhs, Foo const& rhs);

For []

int& operator[] (unsigned i) { return data[i]; }


class A
{
public:
enum enA { A1, A2, A3 };
A() { }
~A() { }

operator A::enA() { return A2; }

};

void Fn(A::enA e)
{
std::cout << "Fn(A::enA)\n";

}

when
A a;
Fn(a); // Will call Fn(A::enA) due to overload of operator A::enA()

C++ Heap Corruption example

class A {

A(int i)
{
text = (char *)malloc(sizeof (char *)) ;
}

A()
{
text = (char *)malloc(sizeof (char *)) ;
}

virtual ~A()
{
if(text != 0)
{
free(text);
text = 0;
}
}

};


void main()
{
A a;
a = A(123);
}

Polymorphism or inheritance watch out

class B {
char *str;
B(){str = new char[100];}
~B(){delete [] str;}
};
class D : public B {
D(){}
~D(){}
};

B* pB = new D;delete pB; // resource leak cause the base destructor (class B) wasn't call.

Solution 1: The base class "MUST" declare destructor as virtual.
e.g.

virtual ~B(){delete [] str;}

Solution 2: (a prevention mechanism to C++) By declaring class as final so that can't inherit the class. C++ itself does not have final, concept taken from Java.

E.g. http://www.codeproject.com/cpp/finalclass.asp


or use

class __declspec(novtable) YourClassName

Excel VBA

A macro I used to creat the hyperlinks to another sheet which is in sequence

Sub CreateLinks()
Dim i, j, NoNeeded As Integeri = 19 'copy source start rowj = 2 'copy destination start rowNoNeeded = 500 'number of linksDim cellSelect As StringDim cellTarget As String
For j = 2 To NoNeededcellSelect = "A" & CStr(j)Range(cellSelect).SelectcellTarget = "!A" & CStr(i)
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=_ "'UNKNOWN (1)'" & cellTarget, TextToDisplay:="'UNKNOWN (1)'" & cellTargeti = i + 1Next j


End Sub

standard C++ OOP features (codes)

#include"stdafx.h"

#include <cstdlib>

#include <iostream>



#define OUT



usingnamespace std;



class A

{

private:

int
iMyNum;

void
APrivateFunc()

{



}



public:

A()

{

iMyNum = 100;

}
friend ostream& operator<<( ostream&, A& ) ; //made as friend function for performance reason



void
MyFunc()

{

printf("myfunct");

}

};ostream&operator<<( ostream& o, A& theObj ){ o << "A" << endl; return o ;}



class B : private A

{





public:

B():A()

{

}

int iMyB;