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.

  • 相关阅读:
    链接
    Oracle创建表空间
    C#中的全局异常捕捉
    软件架构入门
    Nginx安装及配置详解包括windows环境
    极路由4增强版(B70)HC5962离线ROOT通过Breed刷openwrt教程
    vue自定义全局指令v-emoji限制input输入表情和特殊字符
    【vue】@input
    【window】常用软件
    Vbox 虚拟机全屏
  • 原文地址:https://www.cnblogs.com/yzy6806555/p/3069906.html
Copyright © 2011-2022 走看看