2021年2月19日 星期五

讀書心得 - C++ Primer (5th Edition) - Chapter 2 (4) - 常用詞彙

 常用詞彙

  • bind
    Associating a name with a given entity so that uses of the name are uses of the underlying entity. For example, a reference is a name that is bound to an object.
  • compound type
    A type that is defined in terms of another type.
  • const
    Type qualifier used to define objects that may not be changed. const objects must be initialized, because there is no way to give them a value after they are defined.
  • declaration
    Asserts the existence of a variable, function, or type defined elsewhere. Names may not be used until they are defined or declared.
  • definition
    Allocates storage for a variable of a specified type and optionally initializes the variable. Names may not be used until they are defined or declared.
  • const pointer
    Pointer that is const.
  • pointer to const
    Pointer that can hold the address of a const object. A pointer to const may not be used to change the value of the object to which it points.
  • reference to const
    A reference that may not change the value of the object to which it refers. A reference to const may be bound to a const object, a nonconst object, or the result of an expression.
  • const reference
    因為 reference 本身不是 object 所以不能用 const 修飾,所以口語上跟 reference to const 同義。
  • undefined
    Usage for which the language does not specify a meaning. Knowingly or unknowingly relying on undefined behavior is a great source of hard-to-track runtime errors, security problems, and portability problems.(例如給 unsigned char 賦值 256)
  • word
    The natural unit of integer computation on a given machine. Usually a word is large enough to hold an address. On a 32-bit machine a word is typically 4 bytes.

2021年2月18日 星期四

Programmer Interview - stack v.s. heap

 stack v.s. heap

  • 與 threads 的互動
    在一個 multi-threaded 的程式中,每個 thread 都各自擁有一個 stack,但共享一個 heap。
  • object 可以儲存在 heap,而非 stack
    在 c++ 可以使用 new,來將 object 實體儲存在 heap。
        void foo () {
            // myClass, myPointer 儲存在 stack
            // myPointer 所指向的 tempClass object 則儲存在 heap
            // function 結束 myClass, myPointer 則會與 stack 一起 remove
            // 而 myPointer 所指向的 tempClass 不會,所以下 delete
            tempClass myClass;
            tempClass *myPointer = new tempClass();
            delete myPointer;
        }
  • Java 或 .NET 可以透過 garbage collection 來作到 delete myPointer; 的效果。
  • stack 跟 heap 的大小
    stack 大小是固定的,有某些語言可以增加其大小。若 stack 不夠則會造成 stackoverflow (ex 無限遞迴)。heap 大小則是靠 OS 給的。
參考資料 :

Popular Posts