zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    vue-Prop
    C#四舍五入的方法
    设计模式-建造者模式
    vue-解决Vue打包上线之后部分CSS不生效的问题
    vue项目兼容IE浏览器
    html-box-sizing
    MSSQLSERVER执行计划详解
    white-space和word-wrap和word-break所表示的换行和不换行的区别
    JS设置cookie、读取cookie、删除cookie
    windows7 telnet服务开启和登录授权
  • 原文地址:https://www.cnblogs.com/yzy6806555/p/3069906.html
Copyright © 2011-2022 走看看