网志分类列表:programming & research

setting process affinity in windows in command line @ 1/17/2012

If you need to bind a process to some specific cores, you can use the "Set Affinity" function in taskmgr.exe. Open task manager-...
发布于 1/17/2012 11:47:53 | 评论:0

simple command line timer for windows @ 7/27/2011

I have just switched to working on Windows. And I found that there is no such thing as the time command line tool in Linux. So I...
发布于 7/27/2011 15:05:22 | 评论:0

starting / stopping service in windows using command line @ 7/13/2011

In Linux, you typically use "service stop xxx" or "/etc/init.d/xxx stop" for this purpose. Here is the equivalent on windows: ...
发布于 7/13/2011 15:48:24 | 评论:0

simple implementation of all to all communication using send/recv @ 12/23/2010

I was writing a Pregel framework for SCC when I encountered the problem of all to all communication. I used RCCE communication l...
发布于 12/23/2010 18:49:08 | 评论:0

get thread id in pthread @ 12/19/2009

Thread ID starting from 0 is useful when writing multi-threaded program. In OpenMP, you can simply use omp_get_thread_num() to g...
发布于 12/19/2009 21:49:57 | 评论:0

three ways of saying "count lines of code of my project" @ 12/4/2009

C++ code: [code] // C++ code, counts all *.java code in a directory #include <stddef.h> #include <stdio.h> #include <sys/types....
发布于 12/4/2009 10:18:21 | 评论:1

CUDA programming pitfalls 1: atomic operations @ 3/20/2009

I have been using atomic operations in CUDA to implement some lock-free data structures. A few days ago, when I was testing a lo...
发布于 3/20/2009 11:15:54 | 评论:0

在C程序中调用C++代码 @ 5/13/2007

现在在做Open64的编译器项目,里面有一堆80年代SGI写的C代码,现在要加入一些新的代码。我用C++写新的代码,然后试图在C程序中调用,结果出错,在网上搜索半天,终于找到解决办法,这里总结一下。 C与C++的区别主要是C不支持类(Class),不...
发布于 5/13/2007 18:57:22 | 评论:0

Effective STL 3: Make copying cheap and correct for objects in containers @ 5/20/2006

When you insert/push an object into a container, or when you move objects around the container, they get copied. If copying f...
发布于 5/20/2006 19:18:24 | 评论:0

Effective STL 1: Choose your containers with care @ 5/19/2006

Contiguous-memory containers: vector deque string rope (heavy-duty string) Node-based containers: all the others...
发布于 5/19/2006 20:40:13 | 评论:0

More Effective C++ 28: Smart pointers @ 5/8/2006

1. auto_ptr is included in <memory>. 2. assignment is done by ownership transfer [code] auto_ptr<string> p1(new string("hello...
发布于 5/8/2006 19:32:00 | 评论:1

More Effecitve C++ 27: Requiring or prohibiting heap-base objects @ 5/7/2006

1. When created using new, objects are placed on the heap, otherwise on the stack. However, static objects are placed neither on...
发布于 5/7/2006 14:27:35 | 评论:0

More Effective C++ 26: Limiting the number of objects of a class @ 5/2/2006

1. Make the constructors private to control the constructing of objects. 2. Declare public peudo-constructors whichby clients g...
发布于 5/2/2006 16:07:02 | 评论:0

More Effective C++ 25: Virtualize constructors and non-member functions @ 4/27/2006

Though constructors and non-member functions can't be declared virtual, we can make them behave virtually. Consider three cla...
发布于 4/27/2006 11:07:22 | 评论:0

More Effective C++ 24: Understand the costs of virtual functions, multiple inheritance... @ 4/26/2006

Virtual functions are generally implemented by using [i]virtual tables[/i] and [i]virtual table pointers[/i], which are called v...
发布于 4/26/2006 13:45:59 | 评论:0

More Effective C++ 23: Consider alternative libraries @ 4/25/2006

Different libraries offer different performance, so when you have decided that one library is becoming the bottleneck of your pr...
发布于 4/25/2006 21:46:13 | 评论:0

More Effective C++ 22: Consider using op= instead of stand-alone op @ 4/24/2006

The operator += is usually more efficient than + because operator + always returns a new object while += doesn't. For efficienc...
发布于 4/24/2006 13:33:07 | 评论:0

You Raise Me Up @ 4/22/2006

When I am down and all my soul so weary When troubles come and my heart burdened be Then, I am still and waiting in the silenc...
发布于 4/22/2006 13:29:54 | 评论:0

More Effective C++ 21: Overload to avoid implicit type conversions @ 4/22/2006

[code] class UPInt { // class for unlimited public: // p...
发布于 4/22/2006 10:57:43 | 评论:0

More Effective C++ 20: Facilitate the return value optimization @ 4/21/2006

Returning objects by value is always costy, but you can minimize the cost using return value optimization. [code] class Ration...
发布于 4/21/2006 13:24:51 | 评论:0

More Effecitve C++ 19: Understand origin of temporary objects @ 4/19/2006

Temporary objects are generated when: 1. implicit type conversions are applied to make function calls succeed 2. functions ret...
发布于 4/19/2006 14:15:06 | 评论:0

More Effecitve C++ 18: Amortize the cost of expected computation @ 4/18/2006

We disscussed lazy evaluation in the last Item, now we are disscussion over-eager evaluation. Lazy evaluation is used to data t...
发布于 4/18/2006 12:39:57 | 评论:0

More Effective C++ 17: Consider using lazy evaluation @ 4/17/2006

Lazy evaluation sometimes saves resources, and it is applicable in a lot of areas. Here's three of them: 1. Reference Countin...
发布于 4/17/2006 9:59:25 | 评论:0

又见下沙 @ 4/17/2006

今天早上起来,外面一片土黄,知道是又下沙了 庆幸自己昨晚没有开窗,否则今天早上就要被沙子埋了。 自从上次在北信看到一次下沙,已经有两三年了吧。北京一直想治理,可是一直收效甚微。大自然这东西,哪里是渺小的人类可以践踏的呢。砍树的时候怎么就没想过会有今天的...
发布于 4/17/2006 9:46:58 | 评论:1

More Effective C++ 16: Remember the 80-20 rule @ 4/16/2006

The 80-20 rule states that 80 percent of a program's resources are used by 20 percent of the code. So when tuning the performan...
发布于 4/16/2006 19:07:37 | 评论:0

More Effective C++ 15: Understand the costs of exception handling @ 4/15/2006

Exception handling requires a lot of bookkeeping. At each point during execution, programs must keep record of the objects that ...
发布于 4/15/2006 11:37:54 | 评论:0

More Effective C++ 14: Use exception specifications judiciously @ 4/14/2006

What is "exception specification?" [code] void newObject() throw(bad_alloc); // exception specifcation [/code] This excep...
发布于 4/14/2006 13:38:35 | 评论:0

More Effective C++ 13: Catch exceptions by reference @ 4/13/2006

You can catch exceptions by pointer, by value or by reference. 1. Catching exceptions by pointer leaves us with the question ...
发布于 4/13/2006 14:24:39 | 评论:0

More Effective C++ 12: Understand how throwing an exception differs from passing a parameter or ... @ 4/12/2006

Understand how throwing an exception differs form passing a parameter or calling a virtual function. There are three major di...
发布于 4/12/2006 17:20:08 | 评论:0

More Effective C++ 11: Prevent exceptions from leaving destructors @ 4/11/2006

Destructors are called in two situations, normal and abnormal. The [i]normal[/i] situation is when an object goes out of scope, ...
发布于 4/11/2006 13:17:39 | 评论:0

Effective C++ 10: Prevent resource leaks in constructors @ 4/10/2006

[code] class lock{ public: lock(const char * key1,const char * key2){ if(key1!=NULL) theKey1=new ke...
发布于 4/10/2006 14:53:35 | 评论:0

More Effective C++ 9: Use destructors to prevent resource leaks @ 4/7/2006

Exception handling makes us as much trouble as they solve. [code] string * a=new string; try{ ifstream file("kk.txt"); ...
发布于 4/7/2006 14:00:50 | 评论:0

More Effective C++ 8: Understand different meanings of new and delete @ 4/6/2006

There are[i] new operator[/i], [i]operator new [/i]and [i]placement new[/i]. [b]new operator[/b] [code]string * ps=new strin...
发布于 4/6/2006 11:30:11 | 评论:0

More Effective C++ 7: Never overload && || or , @ 4/4/2006

The operators && || and , always evaluate their parameters from left to right. [code]if(p!=NULL && p.size()!=0){...}[/code] ...
发布于 4/4/2006 14:16:00 | 评论:1

More Effecitve C++ 6: Distinguish between postfix and prefix forms of increment and decrement @ 3/31/2006

What's the difference between obj++ and ++obj? [code] class A{ public: ... inline A & operator ++ (){ *this+=1...
发布于 3/31/2006 13:04:18 | 评论:0

More Effective C++ 5: Be wary of user-defined conversion funcitons @ 3/30/2006

Two types of implicit conversions: 1. Single-argument constructors 2. Type conversion operators e.g. [code] class Name{ ...
发布于 3/30/2006 17:41:39 | 评论:0

More Effective C++ 4: Avoid gratuitous default constructors @ 3/29/2006

Sometimes we can't initialize an object without any information. For example, an Emploree class may require an emploree ID, an n...
发布于 3/29/2006 13:28:53 | 评论:0

More Effective C++ 3: Never treat arrays polymorphically @ 3/28/2006

Polymorphism and pointer arithmetic simple don't mix. consider the following code: [code]class Base{...}; class Derived:publi...
发布于 3/28/2006 13:28:46 | 评论:0

More Effective C++ 2: Prefer C++-style casts @ 3/27/2006

There are four types of C++-style casts: dynamic_cast static_cast const_cast reinterpret_cast They are used like this: [...
发布于 3/27/2006 20:55:31 | 评论:0

More Effective C++ 1: Distinguish between pointers and references @ 3/24/2006

Main differences between pointers and references: 1. Pointers [i]should[/i] be initialized while references [i]must[/i]. 2. Po...
发布于 3/24/2006 13:54:03 | 评论:0

Effecitve C++ 50: Improve your understanding of C++ @ 3/15/2006

With this item comes the end of Effective C++ 2nd Edition, a book I have read for over 3 months. I have learnt a lot from this b...
发布于 3/15/2006 11:11:58 | 评论:0

Effective C++ 49: Familiarize yourself with the standard library @ 3/14/2006

1. The specification of C++ standard library takes over 300 closely-packed pages in the °C++ standard. 2. Most of the classes i...
发布于 3/14/2006 16:30:02 | 评论:0

Effective C++ 48: Pay attention to compiler warnings @ 3/14/2006

Unlike compilers of other languages, C++ compilers warns you only when it thinks there is something wrong. For example, here is ...
发布于 3/14/2006 9:06:21 | 评论:0

Effective C++ 47: Ensure that non-local static objects are initialized before they are used @ 3/13/2006

Consider the following code: [code] // file theFileSystem.cpp FileSystem theFileSystem; ... // file Directory.cpp class ...
发布于 3/13/2006 15:53:45 | 评论:0

Effective C++ 46: Prefer compile-time and link-time errors to runtime errors @ 3/10/2006

The problem with runtime errors is that they are unpredictable. You may get no error while testing, but you are not sure whether...
发布于 3/10/2006 22:50:55 | 评论:0

Effective C++ 45: Know what C++ silently writes and calls @ 3/9/2006

C++ will automatically generate constructors, destructors, copy constructors address-of operator and assignment operator for you...
发布于 3/9/2006 10:47:32 | 评论:0

Effective C++ 44: Say what you mean; understand what you're saying @ 3/8/2006

This is a summary of Item 35-43. . A common base class means common traits. . Public inheritance means isa. . Private inherit...
发布于 3/8/2006 12:29:51 | 评论:0

Effective C++ 43: Use multiple inheritance judiciously @ 3/7/2006

I haven't used multiple inheritance before. The reason for that is it's too complex. So my suggestion is to use single inheritan...
发布于 3/7/2006 10:47:58 | 评论:0

Effective C++ 42: Use private inheritance judiciously @ 3/6/2006

[code] class B{ public: void mf(); .... } class D:private B{ ... } void doSomething(B b){ ... } ...
发布于 3/6/2006 13:23:09 | 评论:0

Effective C++ 41: Differentiate between templates and inheritance @ 3/5/2006

A template should be used to generate a collection of class when the types of the objects [i]does not[/i] affect the behavior of...
发布于 3/5/2006 10:53:30 | 评论:0

Effective C++ 40: Model "has-a" or "is-implemented-in-terms-of" through layering @ 3/4/2006

Layering is the process of building a class on top of another by having the layering class contain an object of the layered clas...
发布于 3/4/2006 14:09:38 | 评论:0

Effective C++ 39: Avoid casts down the inheritence hierarchy @ 3/3/2006

Casting down the inheritence hierarchy, also known as downcasting, means casting a pointer or reference of a base class to those...
发布于 3/3/2006 13:45:02 | 评论:0

Effective C++ 38: Never redefine an inherited default parameter value @ 3/2/2006

A default parameter can exist only as part of a function. So if you want to redefine an inherited default parameter value, you h...
发布于 3/2/2006 21:43:21 | 评论:0

Effective C++ 37: Never redefine an inherited nonvirtual function @ 3/1/2006

Let B be the base class, and D be publicly derived from B. If B has a nonvirtual function mf(), then D should not redefine it. ...
发布于 3/1/2006 16:15:02 | 评论:0

Effective C++ 36: Differentiate between inheritance of interface and inheritance of implementation @ 2/28/2006

There are three types of member functions, namely pure virtual functions, simple (nonpure) functions, and nonvirtual functions. ...
发布于 2/28/2006 11:04:54 | 评论:0

Effective C++35: Make sure public inheritance models "isa" @ 2/27/2006

Is penguin a bird? You may say yes, but this is not exactly the case in the realm of C++. [code] class Bird { public: virt...
发布于 2/27/2006 16:11:36 | 评论:0

如何设置cout的输出格式 @ 2/27/2006

设置cout的输出格式有两种办法,一种是用cout的成员变量,一种是用<iomanip>里面提供的函数 比如要设置输出的行宽,就有两种方法: [code] #include<iostram> #include<iomanip> using nam...
发布于 2/27/2006 14:38:20 | 评论:0

Effective C++ 33: Use inlining judiciously @ 2/22/2006

Inlining a function could fasten your program, but only when inlining is judiciouly used. Inlining can also casue your program ...
发布于 2/22/2006 10:23:29 | 评论:0

Effective C++ 32: Postpone variable definitions as long as possible @ 2/21/2006

Most of us are told to define variables at the beginning of a block when we are learning C programming language. Defining all va...
发布于 2/21/2006 14:09:23 | 评论:0

VC++: internal heap limit reached... @ 2/20/2006

自己写了一个类,CSet<Type>,是一个集合类,然后写了一个成员函数 [code]CSet< CSet<Type> > CSet<Type>::GetSubSets()[/code] 用于返回该集合的所有子集组成的集合,但编译时出错了,说"inte...
发布于 2/20/2006 10:47:22 | 评论:1

Effective C++ 30. Avoid member functions that return non-const references or pointers less accessibl @ 2/18/2006

The reason Stafford and Oregon, the author of Effective C++, gave for this is as follows: [quote] The reason for making a memb...
发布于 2/18/2006 10:11:47 | 评论:0

突然感觉到自己的无知 @ 1/19/2006

今天看别人写的代码,其中有一段是这样的: [code] for( i = 0; i < numOfNds; ++i ) { pBNet->AllocFactor(i); CFactor* pFact...
发布于 1/19/2006 15:47:25 | 评论:1

Effective C++ Note 29: Avoid returning "handles" to internal data @ 12/5/2005

What is wrong with the following code? [code]class String { public: String(const char *value); // see Item 11 for po...
发布于 12/5/2005 10:18:08 | 评论:0

Effective C++ Note 28: Use of namespace @ 12/3/2005

Name conflicts have long been a pain for C programers. When you try to include two header files which happen to have the name co...
发布于 12/3/2005 13:02:45 | 评论:0

Effective C++ Note 27: Explicitly disallow use of implicitly generated member functions... @ 12/2/2005

Explicitly disallow use of implicitly generated member functions you don't want Take operator= for example. Sometimes we don'...
发布于 12/2/2005 12:15:31 | 评论:0

Effective C++ Note 26: Guard against potential ambiguity @ 11/28/2005

Potential ambiguity may occur in function overloading and multiple inheriting. [code]void f(char); void f(int); class Base1...
发布于 11/28/2005 22:09:45 | 评论:0

Effective C++ Note 25: Avoid overloading on a pointer and a numberical type @ 11/27/2005

What will happen here? [code]void f(int x){cout<<"int"<<endl;}; void f(string * x){cout<<"string"<<endl;}; void main(){ ...
发布于 11/27/2005 14:51:40 | 评论:0

Effective Note 24: function overloading vs parameter defaulting @ 11/26/2005

The confusion over function overloading and parameter defaulting stems from the fact that they both allow a single function name...
发布于 11/26/2005 22:46:06 | 评论:0

Effective C++ Note 23: Don't try to return a reference when you must return an object @ 11/25/2005

Consider operator+ which always return an object. [code]class C{ C operator + (const C & rhs){ C retval; ...
发布于 11/25/2005 22:38:49 | 评论:0

Effective C++ Note 22: Prefer pass-by-reference to pass-by-value @ 11/24/2005

1. efficiency If you pass an object by value, then inside the function, a copy of the object will be created, which may be rath...
发布于 11/24/2005 23:08:04 | 评论:0

Effective C++ Note 21: Use const whenever possible @ 11/23/2005

Where to use const? [code]const char * a; // const data, non-const pointer char const * a; // same as const char * a char * c...
发布于 11/23/2005 16:20:01 | 评论:0

Effective C++ Note 20: Avoid data members in the public interface @ 11/22/2005

1. for constency If everything in the public interface are functions, clients of your classes won't have to remember whether ...
发布于 11/22/2005 19:56:16 | 评论:0

Effective C++ Note 19: member functions, non-member functions and friend functions @ 11/21/2005

main points: 1. Virtual functions must be member functions. 2. Operator >> and << are never member functions. 3. Only non-mem...
发布于 11/21/2005 22:36:37 | 评论:0

Effecitve C++ Note 18: Strive for class interfaces that are complete and minimal @ 11/20/2005

Complete here means that the clients who use the class can do all reasonable things to the class through the interfaces. And min...
发布于 11/20/2005 11:05:09 | 评论:0

Effective C++ Note 17: Check for assignment to self in operator= @ 11/19/2005

Consider the following code: [code]String a; String & b=a; a=b;[/code] This seems silly, but it is totally legal, and if you...
发布于 11/19/2005 17:35:59 | 评论:0

Effective C++ Note 16: assign to all data members in operator = @ 11/18/2005

When we overload the operator =, it is important that we assign to all data members. This may seem easy when the class is not d...
发布于 11/18/2005 16:40:16 | 评论:0

Effective C++笔记15: 让 operator = 返回一个指向 *this 的引用 @ 11/17/2005

对一个类 C 来说,正确的 operator = 格式应该是这样的: [code]C & C::operator = (const C & rhs){ ... return *this; }[/code] 为什么不返回 voi...
发布于 11/17/2005 10:42:42 | 评论:1

Effective C++ 笔记14: 将基类的析构函数声明为虚函数 @ 11/16/2005

把成员函数声明为虚函数的主要目的是为了支持多态,比如我们有一个基类 Shape 和一个派生类 Circle ,代码如下: [code]class Shape{ public: Shape(){}; virtual ~Shape(){...
发布于 11/16/2005 14:44:22 | 评论:0

Effective C++ 笔记13: 成员变量的初始化顺序 @ 11/15/2005

看看下面的代码: [code]template<class T> class Array { public: Array(int lowBound, int highBound); ... private: vector<T> d...
发布于 11/15/2005 19:55:45 | 评论:1

Effective C++ 笔记12: 在 constructors 中对成员变量采取初始化而不要使用赋值 @ 11/14/2005

有时候我们会写这样的类: [code]template<class T> class NamedPtr { public: NamedPtr(const string& initName, T *initPtr); ... privat...
发布于 11/14/2005 13:55:44 | 评论:0

Effective C++ 笔记11: 为有指针成员的类重载 copy constructor 跟 operator = @ 11/13/2005

因为默认的 copy constructor 和 operator = 都只是简单的对每个成员变量进行拷贝,所以如果有指针变量,它会简单的拷贝指针的内容,造成两个指针指向同一个内存区域。 有一次我就忘了重载 copy constructor,搞了半天...
发布于 11/13/2005 23:41:55 | 评论:0

Effective C++ 笔记10: 在重载操作符 new 的同时重载 delete @ 11/12/2005

[b]为什么要重载 new 和 delete ?[/b] 最主要原因是因为默认的 new 和 delete 函数太慢了,它们要判断很多特殊情况,同时要防止内存碎片的产生……这些工作都是要花时间的。 另外,我们对一个指针进行 delete 操作时,d...
发布于 11/12/2005 17:57:53 | 评论:0

Effective C++ 笔记9: 重载操作符 new 时不要把“正常”的 new 格式给覆盖掉 @ 11/11/2005

如果我们重载了一个类的 operator new,那么我们对这个类进行 new 操作时就一定只会调用重载的 new 函数,看看下面的代码: [code]class A{ public: static void * operator new (...
发布于 11/11/2005 18:38:00 | 评论:0

Effective C++ 笔记8: 重载 new 和 delete 时要遵从的惯例 @ 11/10/2005

有时候我们会重载操作符 new 和 delete (为什么?现在我还没想明白为什么要这么干),这时就要遵从一些惯例。 [b]new 的惯例[/b] 操作符 new 必须接收返回一个 void * 指针,如果申请内存失败则要调用错误处理函数(前一次已经讲...
发布于 11/10/2005 11:50:25 | 评论:3

Effective C++ 笔记7: 对内存不足的处理 @ 11/9/2005

用 new 申请内存时有可能出现内存不足,从而导致申请失败的情况,这时就要采取相应的对策。大多数初学者都会觉得这没什么,毕竟有了虚拟内存技术,一般操作系统的可用内存都会在2G以上,不太可能出现内在不足,但这种可能毕竟是存在的,而一个好的程序就应该把所有可能...
发布于 11/9/2005 22:43:53 | 评论:2

Effective C++ 笔记6: 记得在析构函数中把指针类型的成员变量 delete 掉 @ 11/8/2005

很多类需要动态的使用内存,常见的做法是在类的 constructor 中用 new 申请内存,然后在 destructor 中用 delete 释放内存 但是随着类越写越大,有些人会把 delete 忘掉,以至于造成内存泄漏,要注意,一定要在析构函数中把...
发布于 11/8/2005 13:05:27 | 评论:0

Effective C++ 笔记5: 使用相同形式的 new 和 delete @ 11/7/2005

new 和 delete 有两种形式,带 [] 的和不带 [] 的 比如: [code]string *stringPtr1 = new string; // new without [] string *stringPtr2 = new st...
发布于 11/7/2005 11:38:33 | 评论:0

Effective C++ 笔记4: 用 // 代替 /* */ 做注释 @ 11/6/2005

理由: 1. 打 /**/ 太麻烦,不如 // 来得简单,在做少量注释(不超过一行)的情况下,用 // 能节省时间,而且代码看起来也整齐一点 2. /* 总是找与其最近的 */ 进行配对,所以下面的代码会出错: [code]if ( a > ...
发布于 11/6/2005 20:21:32 | 评论:0

Effective C++ 笔记3: 用 new和delete 代替 malloc和free @ 11/5/2005

malloc和free 是C时代的东西,因此他们并不知道什么是constructors,什么是destructors,这就是它们跟 new/delete 的区别 用malloc分配内存,它的单元不会被初始化,这对一些类来说是不可想像的,比如一个链表的...
发布于 11/5/2005 18:02:13 | 评论:0

Effective C++ 笔记2: 用<iostream>代替<stdio.h> @ 11/4/2005

我想大部分用C++的人都会喜欢 cout 和 cin 吧,比 scanf 和 printf 要简单多了。 scanf 和 printf 的一个致命缺点就是得使用变量的指针(printf要用吗?我忘了,好久不用了),另一个就是把输入输出的格式跟变量混在一起...
发布于 11/4/2005 13:10:52 | 评论:3

Effective C++ 笔记1: 用const和inline代替define @ 11/3/2005

define的使用一般有几种情况: 1. 用来定义常量,如 [code]#define pi 3.1415926[/code] 2. 用来定义小函数,如 [code]#define max(a,b) ((a) > (b) ? (a) : (b))...
发布于 11/3/2005 14:13:02 | 评论:0