顯示具有 Programming Language 標籤的文章。 顯示所有文章
顯示具有 Programming Language 標籤的文章。 顯示所有文章

2009/07/27

C/C++ Predefined Macros

寫程式經常會用到編譯器預先定義的巨集 (Predefined Macros),例如:__FILE__、__LINE__、__DATE__、__TIME__、__func__ 等。這些巨集有些是標準 C/C++ 語言所定義的,有些則是編譯器自行定義的。使用 GNU C/C++ 的人,可參考以下資料:
Predefined Macros (GNU C/C++)
這份資料所提到的 System-specific Predefined Macros 是指針對不同的系統平台 (arm、mips 等),編譯器所預先定義的特殊巨集。若想直接看看這些預先定義的巨集,可執行以下指令:
echo | gcc -dM -E - | sort
使用 Microsoft 系列編譯器的人,可參考以下資料:
Predefined Macros (MSDN)

2009/05/15

String::CRC::Cksum module for 64-bit Perl

When I used the following command to install String::CRC::Cksum on my 64-bit Ubuntu,
perl -MCPAN -e "install String::CRC::Cksum"
the following error message is shown and perl rejected to install it:
Use of uninitialized value in bitwise xor (^) at at /usr/local/share/perl/5.10.0/String/CRC/Cksum.pm line 73.
I managed to google the solution but in vain. Finally, I realized the problem is caused by 64-bit integer on my 64-bit Ubuntu.

To fix this problem, force to install String::CRC::Cksum module first:
$ sudo cpan
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v1.9205)
ReadLine support available (maybe install Bundle::CPAN or Bundle::CPANxxl?)

cpan[1]> force install String::CRC::Cksum
and then patch it (/usr/local/share/perl/5.10.0/String/CRC/Cksum.pm, "chmod" first! it's read-only):
--- Cksum.pm    2009-05-15 00:33:20.000000000 +0800
+++ Cksum.pm 2009-05-15 00:25:36.000000000 +0800
@@ -70,7 +70,7 @@

for(my $i = 0; $i < $n; ++$i) {
my $c = unpack 'C', substr $_[0], $i, 1;
- $cksum = ($cksum << 8) ^ $crctab[($cksum >> 24) ^ $c];
+ $cksum = ($cksum << 8) ^ $crctab[(($cksum >> 24)&255) ^ $c];
++$size;
}

@@ -111,7 +111,7 @@
while($size != 0) {
my $c = $size & 0377;
$size >>= 8;
- $cksum = ($cksum << 8) ^ $crctab[($cksum >> 24) ^ $c];
+ $cksum = ($cksum << 8) ^ $crctab[(($cksum >> 24)&255) ^ $c];
}
$cksum = ~ $cksum;

2008/02/10

Programming Paradigms

重新拿出 The C++ Programming Language 來看 (C++ 的爸爸, Bjarne Stroustrup, 寫的),覺得 Programming Paradigms 這一節寫得真好。

首先,他強調 支援 (support) 和 允許 (enable) 是不相同的:
A language is said to support a style of programming if it provides facilities that make it convenient (reasonably easy, safe, and efficient) to use that style. A language does not support a technique if it takes exceptional effort or skill to write such programs; it merely enables the technique to be used.
在接下來的 programming styles 介紹當中,我們可以更了解這兩個概念的不同之處。

Procedural Programming

這是最早期的程式設計方法,它的法則是:
Decide which procedures you want;
use the best algorithms you can find.
也就是說,先想好一件工作需要由哪些 procedures 完成;再用最好的演算法完成這些 procedures。Fortran 算是 support 這種程式設計方式的始祖,C 也 support 這種設計方法。

Modular Programming

隨著程式規模愈來愈大,程式設計開始重視如何組織資料。將資料和處理這些資料的 procedures 結合在一起,稱之為 module。這種程式設計方法的法則是:
Decide which modules you want;
partition the program so that data is hidden within modules.
也就是說,以資料為中心,將相關的資料和 procedures 結合成各自獨立的 module。Module 和 module 之間,資料是隱藏起來的,所以此種法則也叫做 data-hiding principle。Modula-2 直接 support 這種程式設計方式,但 C 只能算是 enable 這種方法。

Data Abstraction

在某些應用場合,data hiding 這個觀念仍然不夠理想,必須導入 abstract data types 的觀念 (也就是 user-defined types)。這種程式設計方法的法則是:
Decide which types you want;
provide a full set of operations for each type.
也就是說,先想好有哪些資料型態要操作,然後為每一個資料型態設計完整的操作程序。

Object-Oriented Programming

對於類似圖形使用者介面的系統,data abstraction 缺乏彈性和擴充能力。這時,就需要導入 object-oriented 這種可以繼承類別的觀念。這種程式設計方法的法則是:
Decide which classes you want;
provide a full set of operations for each class;
make commonality explicit by using inheritance.
也就是說,先想好有哪些類別要操作,然後為每一個類別設計完整的操作程序;類別和類別之間的共通特性要以繼承的方式來處理。

Generic Programming

這種程式設計方法的法則是:
Decide which algorithms you want;
parameterize them so that they work for
a variety of suitable types and data structures.
這一部份,我還要多多努力學習才行。

2008/02/09

TIOBE Programming Community Index

TIOBE Programming Community Index 是一份很有趣的統計資料。它利用搜尋引擎來統計 programming language 的熱門程度,每個月更新一次。除了 2005 年前後被 C 打敗之外,Java 已經蟬聯盟主寶座多年,C 則穩居第二名。

排名總是會引起誤解。例如:Java 比 C/C++ 好。我想這是非常錯誤的觀念。Programming language 只是個工具,不同場合需要不同的工具。如此而已。

套句廣告台詞:它傻瓜,你聰明。會使用對的 programming language 來解決問題,才是聰明的程式設計師。