• What Every Computer Programmer Should Know About Windows API, CRT, and the Standard C++ Library


    When the CRT/C++ Library is linked statically, then all its code is embedded into the resulting executable image. The problem is that internal CRT objects cannot be shared with other CRT instances. The memory allocated in one instance of CRT must be freed in the same instance, the file opened on one instance of CRT must be operated and closed by functions from the same instance, etc. It happens because the CRT tracks the acquired resources internally. Any attempt to free a memory chunk or read from a file via FILE* that came from another CRT instance will lead to corruption of the internal CRT state and most likely to crash.

    That's why linking CRT statically obligates a developer of a module to provide additional functions to release allocated resources and a user of a module to remember to call these functions in order to prevent resource leaks. No STL containers or C++ objects that use allocations internally can be shared across modules that link to the CRT statically. The following diagram illustrates the usage of a memory buffer allocated via a call to malloc.

     

    Diagram #2: Using memory allocated by malloc from different modules

    Diagram #2: Using memory allocated by malloc' from different modules

     

    In the above diagram, Module 1 is linked to the CRT statically, while Modules 2 and 3 are linked to the CRT dynamically. Modules 2 and 3 can pass CRT owned objects between them freely. For example, a memory chunk allocated with malloc in Module 3 can be freed in Module 2 with free. It is because both malloc and free calls will end up in the same instance of CRT.

    On the other hand, Module 1 cannot let other modules to free its resources. Everything allocated in Module 1 must be freed by Module 1. It is because only Module 1 has access to the statically linked instance of the CRT. In the above sample, Module 2 must remember to call a function from Module 1 in order to properly release the acquired memory.

  • 相关阅读:
    AutoIT练习
    AutoIT键盘鼠标模拟组件
    javascript class 定义
    JIRA
    vs 命令窗口 常用命令
    jquery 获取参数 plugin
    THE VALUE OF TIME
    哈佛图书馆的二十条训言
    优秀javascript js组件集锦
    UVA 11205 The broken pedometer
  • 原文地址:https://www.cnblogs.com/yzy6806555/p/3069906.html
Copyright © 2020-2023  润新知