• [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.


  • 相关阅读:
    从DataGridViewer导出Excel
    C#获取特定进程CPU和内存使用率
    51 nod 1265 四点共面
    51nod 1384 全排列
    51nod 2006 飞行员配对
    codeforces 839A
    codeforce 837C
    codeforces 837B
    codoforces 837A
    GIT
  • 原文地址:https://www.cnblogs.com/codingtmd/p/5078908.html
Copyright © 2020-2023  润新知