Search

10/31/2006

C++ Reference

    int ival = 1024;
int *pi = &ival;
int &rval = ival;

cout << ival << endl;
cout << rval << endl;
cout << *pi << endl;
cout << &ival << endl;
cout << &rval << endl; //rval跟ival是同一各記憶體位址
cout << pi << endl; // pi儲存的是ival的記憶體位址
cout << & pi << endl; // pi也是個變數, 有自己的記憶體位址
// cout << *rval << endl; // error
// cout << *ival << endl; // error

以下是輸出結果
1024
1024
1024
0xbf9ec99c
0xbf9ec99c
0xbf9ec99c
0xbf9ec998

把 int &rval = ival; 換成 int &rval = *pi; 結果一樣。

10/27/2006

Automatix

http://www.getautomatix.com/

Automatix is a graphical interface for automating the installation of the most commonly requested applications in Debian based linux operating systems.

Linux.com : Automatix kicks Ubuntu into gear
Ubuntu 2006.10(Edgy)的安裝過程在這裡, 包含的套件在這裡

10/25/2006

本週閱讀2006.10.25-2006.10.31

10/23/2006

Pointers and Constants

以下內容摘自The C++ Programming Language

void f1(char *p){
char s[] = "Gorm";

const char *pc = s;
pc[3] = 'g'; // error: pc points to constant
pc = p; // ok

char *const cp = s;
cp[3] = 'a'; // ok
cp = p; // error: cp is constant

const char *const cpc = s;
cpc[3] = 'a'; // error: cpc points to constant
cpc = p; // error: cpc is constant
}

char *const cp;  //const pointer to char
char const* pc; //pointer to const char
const char* pc2; //pointer to const char

to read each such declarations right-to-left. ex: "cp is a const pointer
to a char" and "pc2 is a pointer to a char const".

void f4(){
int a = 1;
const int c = 2;
const int *pl = &c; // ok

const int *p2 = &a; // ok
int *p3 = &c; // error: initialization of int* with const int*
*p3 = 7;
}

10/21/2006

Re: [問題] C/C++中char*與char[]的差異

Re: [問題] C/C++中char*與char[]的差異

其實只要 C code 裡面出現 "string" 這種 literal constant string,
那 's' 't' 'r' 'i' 'n' 'g' '\0' 這個 sequence 必會出現在 constant pool 中,
而當你寫 char str[] = "string"; 且 str 非 global variable 時,
compiler 並不會直接產生一群分別填入這些字元到 stack 上的 code,
而是產生 load 之類的指令將這些值從 constant pool 搬至 stack。

Re: [問題] C/C++中char*與char[]的差異

"xxxxxx" 都是放在 constant pool 中,
而且通常 compiler 會將 constant pool 設為 read only。

char *ptr = "xxxxxx"; 是讓 ptr 指向 constant pool 裡第一個 'x' 的位置。
char str[] = "xxxxxx"; 是在 stack 中開 7 個 bytes 的空間,
並將 'x' 'x' 'x' 'x' 'x' 'x' '\0' 七個字元從 constant pool 中複製過來。

10/16/2006

Calling Conventions Demystified

Calling Conventions Demystified
__cdecl,__fastcall, __stdcall 什么区别?
c/c++的參數壓棧順序

* __cdecl is the default calling convention for C and C++ programs. The advantage of this calling convetion is that it allows functions with a variable number of arguments to be used. The disadvantage is that it creates larger executables.
* __stdcall is used to call Win32 API functions. It does not allow functions to have a variable number of arguments.
* __fastcall attempts to put arguments in registers, rather than on the stack, thus making function calls faster.
* Thiscall calling convention is the default calling convention used by C++ member functions that do not use variable arguments.

本週閱讀2006.10.15-2006.10.21

Google Print secret revealed

  • Google Print secret revealed
  • [新知]天價級的書本掃描器
  • Google Book Search Project
    Google recently implemented a convoy of new machines from Kirtas Technologies called the APT BookScan 1200, which is capable of scanning up to 1200 pages per hour (Kirtas Technologies). The machine works by utilizing a 16-megapixel digital camera to photograph each page, then transfers the image to local storage. Then, a robotic arm gently turns the page of the book, and the process is completed for each additional page. The digital images are then ‘cleaned’ to remove smudges and other errors, cropped, and centered. At this point, the image can be posted online, but "searching through the text is impossible.

雖然有comment說不是這一台,不過至少可以確定不是人工掃瞄的了。

面試相關文章

Quick tip: Styling blockquotes with CSS

blockquote {
background: transparent url(quoleft.png) left top no-repeat;
}
blockquote div {
padding: 0 48px;
background: transparent url(quoright.png) right bottom no-repeat;
}

10/15/2006

[轉載]學程式設計的人不能不看的好文章


以pow為例,pow簡單好寫,不過常常會忘了有效率的寫法:
1. 最直覺的方法,iterative,一個一個乘,把結果傳回去。
int pow(int base, int power){ 
int result = base;

if(power==0)
return 1;
while(power-->1)
result *= base;

return result;
}

2. 用recusive來做,寫出來的比較容易理解。
int pow(int base, int power){
if (power==0)
return 1;
return base*pow(base, power-1);
}

3. 最後就是第一題考的東西。
int pow(int base, int power){

if(power==0)
return 1;
if(power%2==0)
return pow(base, power/2)*pow(base, power/2);
else
return base*pow(base, power-1);
}

4. 可能再快一點的寫法,不過這不重要了。
int pow(int base, int power){
int t;

if(power==0)
return 1;
if(!(power&1)){
t = pow(base, power/2);
return t*t;
}
else
return base*pow(base, power-1);
}

Union

  • a union is big enough to hold the wildest member.
  • a union is a structure in which all memebers have offset zero from the base.

利用union判斷是Big-endian or Litte-endian (from : C, A Reference Manual)
#include<stdio.h>
union{
long l;
char c[sizeof(long)];
}u;

int main(){
u.l = 1;
if(u.c[0] == 1)
printf("Addressing is right-to-left(Little-endian)\n");
else if (u.c[sizeof(long)-1] == 1)
printf("Addressing is left-to-right(Big-endian)\n");
else printf("Addressing is strangs\n");
}

32bit數0x12345678在litte-endian存放方式(假設從0x4000開始存放)
0x4000 0x78
0x4001 0x56
0x4002 0x34
0x4003 0x12

32bit數0x12345678在big-endian存放方式(假設從0x4000開始存放)
0x4000 0x12
0x4001 0x34
0x4002 0x56
0x4003 0x78

10/08/2006

本週閱讀2006.10.08-2006.10.14

10/02/2006

Calculating Fibonacci numbers

http://www.ics.uci.edu/~dan/class/161/notes/7/Fib.html

Divide-and-conquer:

Fib3(n)
i := 1
j := 0
k := 0
h := 1
while n > 0 do
if n is odd then
t := jh
j := ih + jk + t
i := ik + t
t := h2
h := 2kh + t
k := k2 + t
n := floor( n/2 )
return j

T(n) = Θ(log n)

Fib3 works by making use of the fact that
_ _ n _ _
| 0 1 | | fn-2 fn-1 |
| | = | |
|_ 1 1 _| |_ fn-1 fn _|

10/01/2006

codefreak

http://fsfoundry.org/codefreak/

  • C++ Terminology - 1. Variety of Types

    Template Class
    也是 user-defined type 的 subset. 為 class template 的 instance
    (具現), e.g.:
    typedef list IntList;


    POD

    Acronym for Plain Old Data. User-defined C-style structure. 為
    user-defined type 的 subset, 需符合下列條件:
    1. 沒有 user-defined constructor, destructor 以及 assignment operator
    2. 沒有 base class type, 換句話說非 derive 自 user-defined type
    3. 如果有 data member, 其所有 data member 必須為 fundamental type 或 POD
    下兩例皆為 POD:

    class SomePodType
    {
    public:
    unsigned int data0;
    };
    struct AnotherPodTypeWithDataMember
    {
    SomePodType data1;
    char data2;
    };


    User-defined Type

    Aka. class type. 使用者自訂型別, 也稱為類或類別. 泛指所有的 struct 與
    class, 包含 C++ Standard Library 定義的型別, e.g.:

    class SomeClass;
    struct SomeStruct;
    template class basic_string;

    對 C++ 來說, 在定義 user-defined type 時, class 與 struct 幾乎是沒有差別且
    能互相替換 (interchangable). 唯一的差別是預設 (默認) 的存取權限:

    class DerivedClass : /*private*/ BaseClass
    {
    /*private:*/
    int data;
    };
    struct DerivedStruct : /*public*/ BaseStruct
    {
    /*public:*/
    int data;
    };

  • C++ Terminology - 2. Instance and Instanciation
  • Misconception of C++ Efficiency
  • Is your Singleton Broken?
  • Pointer to What?
    foo const * b;
    定義一個名為 b 的 (非常數) 指標, 這個指標指向一個常數 (const) foo.
    foo * const c;
    定義一個名為 c 的常數 (const) 指標, 這個指標 指向一個 (非常數) foo.

  • Boost.Lambda
  • C++0x
  • C/C++ Tips - The Marco Assert
    assert() 是個用來 debug 的macro, 接受一個 expression. 如果 expression 被 evalute 的結果為整數零, 或能被 convert 成零 (如 null pointer), 則會引發 assertion failure.
    assert(str != NULL); //str == NULL時會引發assert

Travelstar 4K40 4k120 hard disk drives specifications

http://www.hitachigst.com/hdd/support/4k40/4k40.htm

Requirement   +5VDC(±5%)
Startup current (peak, max.) 4.7 W
Seek (avg.) 2.3 W
Read (avg.) 2.0 W
Write (avg.) 2.1 W
Performance idle (avg.) 1.85 W
Active idle (avg.) 0.85 W Low power idle (avg.) 0.65 W
Standby (avg.) 0.25 W
Sleep 0.1 W

http://www.hitachigst.com/hdd/support/4k120/4k120.htm
Requirement   +5VDC(±5%)
Startup (peak, max.) 4.5W
Seek 1.7W
Read (avg.) 1.4W
Write (avg.) 1.4W
Performance idle (avg.) 1.25W
Active idle (avg.) 0.65W
Low power idle (avg.) 0.45W
Standby (avg.) 0.15W
Sleep 0.1W