什么是C++的内在函数

     想必很多学C++的都没听说过内在函数,我也是初次听见。什么是内在函数?英文是Intrinsic Function。这是一种编译器特殊函数,它的行为类似与C++的内联函数,但是由于它是编译器提供的,所以编译器对其更加了解更利于优化。比如说memcpy等就是内在函数。如果编译器有选择/Optimization,那么编译器在生成代码的时候将直接插入运行代码而不是调用该函数,这和内联函数差不多,但是如上所说,由于编译器对其的了解,这个优化会更好。
       在vc++中,AbnormalTermination也是一个内在函数。
       下面是维基百科的一些介绍:
       This article is about compiler intrinsic functions. For X toolkit, see Intrinsics.
       In compiler theory, an intrinsic function is a function available in a given language whose implementation is handled specially by the compiler. Typically, it substitutes a sequence of automatically-generated instructions for the original function call, similar to an inline function. Unlike an inline function though, the compiler has an intimate knowledge of the intrinsic function and can therefore better integrate it and optimize it for the situation. This is also called builtin function in many languages.
       Compilers that implement intrinsic functions generally enable them only when the user has requested optimization, falling back to a default implementation provided by the language runtime environment otherwise.
       Intrinsic functions are often used to explicitly implement vectorization and parallelization in languages which do not address such constructs. Altivec and OpenMP are examples of APIs which use intrinsic functions to declare, respectively, vectorizable and multiprocessor-aware operations during compilation. The compiler parses the intrinsic functions and converts them into vector math or multiprocessing object code appropriate for the target platform.
       Microsoft and Intel's C/C++ compilers as well as GCC implement intrinsics that map directly to the x86 SIMD instructions (MMX, SSE, SSE2, SSE3, SSSE3, SSE4). In the latest version of the Microsoft compiler (VC2005 as well as VC2008) inline assembly is not available when compiling for 64 bit Windows, therefore intrinsics are necessary. To compensate for the lack of inline assembly, new intrinsics have been added that map to standard assembly instructions that are not normally accessible through C/C++ (e.g.: bit scan).

### 回答1: c++ 中的 delete 函数是用来释放动态分配的内存的。它有两种形式: 1. delete p:用于释放单个对象所占用的内存。 2. delete[] p:用于释放数组对象所占用的内存。 在释放内存之前,delete 会调用对象的析构函数,以便清理对象占用的资源。 delete 的内在机制是通过调用 operator delete 来实现的。operator delete 是一个函数指针,指向用于释放内存的函数。operator delete 的默认实现是系统函数,但是可以被用户自定义。 注意:在使用 delete 释放内存之前,应该确保这块内存是通过 new 运算符动态分配的。否则,可能会出现未定义的行为。 ### 回答2: C++中的delete函数用于释放动态分配的内存。它会按照以下步骤进行内存释放: 首先,delete函数会执行被删除对象的析构函数。析构函数是一个特殊的成员函数,用于清理对象中可能存在的资源。通过调用析构函数,对象将会被正确释放,并且其成员变量所占用的内存也会被释放。 接下来,delete函数会释放对象所占用的内存。它会返回该内存区域给系统的内存管理器,并且标记该内存可用。 需要注意的是,delete函数只能释放通过new动态分配的内存。如果尝试使用delete来释放不是通过new分配的内存,会导致未定义的行为。 当多个delete被用于同一个指针时,会导致程序崩溃或内存泄漏。因此,在使用delete释放内存后,应该将指针置为nullptr,以避免对同一块内存进行重复删除操作。 ### 回答3: C语言中的delete函数内在机制是通过释放动态分配的内存空间来实现的。在C语言中,如果我们使用malloc函数或者calloc函数来动态分配内存空间,那么就必须使用free函数来释放这些内存空间,以避免内存泄露。 当我们调用delete函数时,它首先会检查指针是否为空,如果为空则不进行任何操作。如果指针不为空,delete函数会找到与指针对应的内存块,并进行释放。 delete函数的内部机制首先会调用对象的析构函数来进行资源的释放。析构函数是在对象生命周期结束时被调用的,它负责释放对象占用的资源以及清理与对象相关的任何操作。然后,delete函数会调用free函数来释放对象所占用的内存空间。 在释放对象的内存空间后,delete函数会将指针置为空,以避免悬空指针的问题,即指针指向了已经释放的内存空间。 需要注意的是,delete函数只能释放通过new运算符动态分配的内存空间,而不能用来释放通过malloc或calloc函数分配的内存空间。使用delete释放不是通过new分配的内存空间或者重复释放同一块内存空间,可能会导致未定义的行为,例如内存泄露或者程序崩溃。 总结起来,delete函数内在机制主要是通过调用对象的析构函数来释放资源,并调用free函数来释放对象所占用的内存空间,同时将指针置为空,以确保程序的安全和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值