[C++ Summary]

1. Pass by pointer VS reference

A pointer can receive a NULL parameter, a reference parameter can not. If there's ever a chance that you could want to pass "no object", then use a pointer instead of a reference.
Also, passing by pointer allows you to explicitly see at the call site whether the object is passed by value or by reference:
// Is mySprite passed by value or by reference?  You can't tell 
// without looking at the definition of func()
func
(mySprite);

// func2 passes "by pointer" - no need to look up function definition
func2
(&mySprite);




2. Read following code and write the output result

1:  int _tmain()  
2: {
3: char s[256];
4: char *p =s;
5: p = "some new string";
6: cout<<s<<endl;
7: cout<<*s<<endl;
8: cout<<*(s+2)<<endl;
9: cout<<&p<<endl;
10: cout<<*p<<endl;
11: cout<<*(p+2)<<endl;
12: return 0;
13: }

Output:
any string in memeory

empty
empty
0080F820
s
m



3. Interface VS abstract class in C++

An interface is a contract, you can implement few interfaces for one class. As oppose to abstract class, you can extend only one. So, if you want to communicate with few programs interface would help you, like (in java) comparable and something else. And in C++, it doesn't have Interface key word, only pure virtual methods
The advantage of abstract class is that you can already implement the methods that are relevant for that class. So, when you inherit that class you will already have these methods without the need to duplicate the code if you use an interface.



4. new VS malloc in C++

From the C++ FQA Lite:
[16.4] Why should I use new instead of trustworthy old malloc()?
FAQ: new/delete call the constructor/destructor; new is type safe, malloc is not; new can be overridden by a class.
FQA: The virtues of new mentioned by the FAQ are not virtues, because constructors, destructors, and operator overloading are garbage (see what happens when you have no garbage collection?), and the type safety issue is really tiny here (normally you have to cast the void* returned by malloc to the right pointer type to assign it to a typed pointer variable, which may be annoying, but far from "unsafe").
Oh, and using trustworthy old malloc makes it possible to use the equally trustworthy & old realloc. Too bad we don't have a shiny new operator renew or something.
Still, new is not bad enough to justify a deviation from the common style used throughout a language, even when the language is C++. In particular, classes with non-trivial constructors will misbehave in fatal ways if you simply malloc the objects. So why not use new throughout the code? People rarely overload operator new, so it probably won't get in your way too much. And if they do overload new, you can always ask them to stop.

5. How to implement c++ smart pointer?

http://www.codeproject.com/Articles/15351/Implementing-a-simple-smart-pointer-in-c


6. Pro and Cons of multi-process and multi-thread?
So the key difference between processes and threads is the way memory is managed. This has several implications, the two of the most most important are:

  • Inter-thread communication is fast
  • There is no protection between threads

Since processes do not naturally share memory it is difficult for one process to communicate with another. Several Inter-Process Communications (IPC) methods exist but they all rely on passing data via some intermediary such as the file system or network stack. Ultimately the kernel manages communications between them.

Threads, on the other hand, can communicate directly using shared memory objects such as arrays of data (buffers).

The disadvantage of threads is the classic problem faced by engineers used to RTOS's and that is the fact that a bug in one thread can corrupt the memory being used by another thread. When a thread crashes it is natural to start debugging that thread but it may well be that the bug is in code utilised only by another thread. These issues can be very difficult to track down!

So when designing an application to exploit the parallel capability of hardware it may seem that the decision is one of performance over protection and debug-ability. But it is not always so simple.

You could have a fast web server, making heavy use of the threaded capability of the OS, but it is unlikely that you will have many customers if they are running an on-line sales system. Why? Simply that any exploitable defect could enable an attacker to read the credit card data from another user - remember that the memory for the thread handling that transaction is readable by the thread handling the attackers connection.


转载于:https://www.cnblogs.com/codingtmd/archive/2013/02/03/5078908.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值