new和malloc关键字

·
notes no tag March 9, 2022

数据结构中有一个问题,链表后插。

这个操作需要新建节点,自然要用到new或者malloc关键字。c中较为严谨,在malloc后需要判断是否能新建节点成功(LNode2 *s2=(LNode2*) malloc(sizeof(LNode2));),当然99.99%是能成功的,除非计算机内存实在不够了...

于是临下班前试着用c++实现一遍,换成类来描述,这个判断自然就变成了

    LNode *S = new LNode();
    if (S == nullptr)
        return false;//内存不足分配失败

问题就出在这里了。IDE(Clion)提示我"Condition is always false",并且VS好像还不会报出来这个。

因此下来研究、学习了一下。C++中operator new的行为及其与malloc的区别。new在实现中会调用malloc并且由编译器安插调用构造函数的代码,malloc失败返回0,而new直接抛异常。但不清楚有没有不抛异常的new和不调用构造函数的new这一点。下面结合代码分析一下。

19:     CTest *pTest = new CTest(3, 2);
0027652D 6A 08                push        8  
0027652F E8 8D AD FF FF       call        operator new (02712C1h)  ; 默认的operator new
00276534 83 C4 04             add         esp,4  
00276537 89 85 08 FF FF FF    mov         dword ptr [ebp-0F8h],eax  
0027653D C7 45 FC 00 00 00 00 mov         dword ptr [ebp-4],0  
00276544 83 BD 08 FF FF FF 00 cmp         dword ptr [ebp-0F8h],0  
0027654B 74 17                je          main+74h (0276564h)  
0027654D 6A 02                push        2  
0027654F 6A 03                push        3  
00276551 8B 8D 08 FF FF FF    mov         ecx,dword ptr [ebp-0F8h]  
00276557 E8 B4 AE FF FF       call        CTest::CTest (0271410h)  ; 调用构造函数
0027655C 89 85 E8 FE FF FF    mov         dword ptr [ebp-118h],eax  
00276562 EB 0A                jmp         main+7Eh (027656Eh)  
00276564 C7 85 E8 FE FF FF 00 00 00 00 mov         dword ptr [ebp-118h],0  
0027656E 8B 85 E8 FE FF FF    mov         eax,dword ptr [ebp-118h]  
00276574 89 85 14 FF FF FF    mov         dword ptr [ebp-0ECh],eax  
0027657A C7 45 FC FF FF FF FF mov         dword ptr [ebp-4],0FFFFFFFFh  
00276581 8B 8D 14 FF FF FF    mov         ecx,dword ptr [ebp-0ECh]  
00276587 89 4D EC             mov         dword ptr [pTest],ecx  
    20: 
    21:     CTest *pTest2 = new (std::nothrow) CTest(4, 6);
0027658A 68 D0 C2 27 00       push        offset std::nothrow (027C2D0h)  
0027658F 6A 08                push        8  
00276591 E8 63 AC FF FF       call        operator new (02711F9h)  ;多个参数,不抛异常的operator new
00276596 83 C4 08             add         esp,8  
00276599 89 85 F0 FE FF FF    mov         dword ptr [ebp-110h],eax  
0027659F C7 45 FC 01 00 00 00 mov         dword ptr [ebp-4],1  
002765A6 83 BD F0 FE FF FF 00 cmp         dword ptr [ebp-110h],0  
002765AD 74 17                je          main+0D6h (02765C6h)  
002765AF 6A 06                push        6  
002765B1 6A 04                push        4  
002765B3 8B 8D F0 FE FF FF    mov         ecx,dword ptr [ebp-110h]  
002765B9 E8 52 AE FF FF       call        CTest::CTest (0271410h)  ; 调用构造函数
002765BE 89 85 E8 FE FF FF    mov         dword ptr [ebp-118h],eax  
    20: 
    21:     CTest *pTest2 = new (std::nothrow) CTest(4, 6);
002765C4 EB 0A                jmp         main+0E0h (02765D0h)  
002765C6 C7 85 E8 FE FF FF 00 00 00 00 mov         dword ptr [ebp-118h],0  
002765D0 8B 85 E8 FE FF FF    mov         eax,dword ptr [ebp-118h]  
002765D6 89 85 FC FE FF FF    mov         dword ptr [ebp-104h],eax  
002765DC C7 45 FC FF FF FF FF mov         dword ptr [ebp-4],0FFFFFFFFh  
002765E3 8B 8D FC FE FF FF    mov         ecx,dword ptr [ebp-104h]  
002765E9 89 4D E0             mov         dword ptr [pTest2],ecx  
    22: 
    23:     pTest->m_dw1 = 3;
002765EC 8B 45 EC             mov         eax,dword ptr [pTest]  
002765EF C7 00 03 00 00 00    mov         dword ptr [eax],3  
    24:     pTest2->m_dw2 = 5;
002765F5 8B 45 E0             mov         eax,dword ptr [pTest2]  
002765F8 C7 40 04 05 00 00 00 mov         dword ptr [eax+4],5  
    25:     
    26:     
    27:     return 0;
002765FF 33 C0                xor         eax,eax  
    28: }

operator new的源码:

void* __CRTDECL operator new(size_t const size)
{
    for (;;)
    {
        if (void* const block = malloc(size))
        {
            return block;
        }

        if (_callnewh(size) == 0)
        {
            if (size == SIZE_MAX)
            {
                __scrt_throw_std_bad_array_new_length();
            }
            else
            {
                __scrt_throw_std_bad_alloc();
            }
        }

        // The new handler was successful; try to allocate again...
    }
}

operator new的源码,嗯嗯,果然不可能抛异常,因为异常都被接住了:

void* __CRTDECL operator new(size_t const size, std::nothrow_t const&) noexcept
{
    try
    {
        return operator new(size);
    }
    catch (...)
    {
        return nullptr;
    }
}

delete操作都是一样的,调用了带两个参数的operator delete,这个应该也是后来搞出来的一个函数,我印象还比较深。之前在尝试将VS2022编译的obj与WDK中lib版CRT库匹配时曾遇到过这个带了两个参数的delete找不到匹配的符号,结果用了/Zc:sizedDealloc-开关才避免其生成。

   26:  delete(pTest);
013865FF 8B 45 EC             mov         eax,dword ptr [pTest]  
01386602 89 85 E4 FE FF FF    mov         dword ptr [ebp-11Ch],eax  
01386608 6A 08                push        8  
0138660A 8B 8D E4 FE FF FF    mov         ecx,dword ptr [ebp-11Ch]  
01386610 51                   push        ecx  
01386611 E8 3F AA FF FF       call        operator delete (01381055h)  
01386616 83 C4 08             add         esp,8  
    27:  delete(pTest2);
01386619 8B 45 E0             mov         eax,dword ptr [pTest2]  
0138661C 89 85 D8 FE FF FF    mov         dword ptr [ebp-128h],eax  
01386622 6A 08                push        8  
01386624 8B 8D D8 FE FF FF    mov         ecx,dword ptr [ebp-128h]  
0138662A 51                   push        ecx  
0138662B E8 25 AA FF FF       call        operator delete (01381055h)  
01386630 83 C4 08             add         esp,8  

在其中调用了带一个参数的operator delete:

    28: void __CRTDECL operator delete(void* const block, size_t const) noexcept
    29: {
01384FF0 55                   push        ebp  
01384FF1 8B EC                mov         ebp,esp  
    30:     operator delete(block);
01384FF3 8B 45 08             mov         eax,dword ptr [block]  
    30:     operator delete(block);
01384FF6 50                   push        eax  
01384FF7 E8 36 C0 FF FF       call        operator delete (01381032h)  
01384FFC 83 C4 04             add         esp,4  
    31: }
01384FFF 5D                   pop         ebp  
01385000 C3                   ret  

结论:可以通过std::nothrow让new不抛异常,VS22下的做法是在该new函数中用try捕获了异常,并不再抛出,同时让其返回0;对于有构造函数的类对象,new时都会调用其构造函数,是由编译器自动插入代码的。

最最后,JetBrains真香。

  • 指针引用带来的思考
  • 带指针类的初始化

已有 26 条评论
  1. agevose

    Cialis Hcpcs Code Cephalexin Injection

    agevose July 27th, 2022 at 07:09 am回复
  2. floucoura

    ivermectin for cats Cialis Apotheken Umschau

    floucoura August 15th, 2022 at 02:08 pm回复
  3. vulsilm

    Sildenafil citrate is the chemical name of the active ingredient in Viagra cialis pills While that mindset has changed, doctors perform most skin tag removal treatments

    vulsilm September 1st, 2022 at 12:00 am回复
  4. Swiquew

    buy priligy in the us Rs 80 Strip Get Latest Price

    Swiquew September 3rd, 2022 at 03:08 am回复
  5. trarnence

    It s widely available and well known is there a generic cialis available In most cases, if you would take this medication daily, then your doctor might prescribe a lower dose of 60 to 60 mg

    trarnence September 5th, 2022 at 01:41 am回复
  6. Tradway

    buy cialis online uk If you are shopping for Cialis online, be sure to include shipping charges for all other packages eg

    Tradway September 7th, 2022 at 03:57 am回复
  7. angewly

    We respect everyone s right to express their thoughts and opinions as long as they remain respectful of other community members, and meet What to Expect s Terms of Use. buy clomid without prescription uk Deficiency of spleen and kidney shu di 30 g, ba ji 30 g, fried bai zhu 30 g, ren shen 15 g, raw huang qi 15 g, shan yu rou 9 g, gou qi zi 6 g, chai hu 1.

    angewly September 10th, 2022 at 01:36 am回复
  8. Veiruct

    Google Scholar. side effects of tamoxifen after 5 years

    Veiruct September 13th, 2022 at 03:36 am回复
  9. hereulley

    7 Characteristics of hospitalization No- invasive mechanical ventilation 66 1. difference between doxycycline hyclate and monohydrate To assess the efficiency of HPV16 E6 and TP53 depletion, TP53 protein levels were assessed by western blotting.

    hereulley September 16th, 2022 at 06:10 pm回复
  10. errowly

    lasix mechanism of action Is the elevated levels normal temporary and will it probably go down

    errowly October 7th, 2022 at 03:09 am回复
  11. toinnafar

    food exports already flowing into the country undera 2000 amendment to sanctions clomid for men side effects

    toinnafar November 16th, 2022 at 04:03 am回复
  12. stoowatry

    Congress plunged the nation into a partial government shutdown Tuesday as a long running dispute over President Barack Obama s health care law stalled a temporary funding bill, forcing about 800, 000 federal workers off the job and suspending most non essential federal programs and services how to get stromectol 00 h 83, although diurnal variation is substantially blunted in older men

    stoowatry November 18th, 2022 at 12:31 am回复
  13. Haurire

    tetracycline vs doxycycline Furthermore, expression of MMP9 was increased whereas TIMP1 was decreased in lesion segments in these animals

    Haurire November 21st, 2022 at 05:08 pm回复
  14. coancepaL

    how do you spell lasix Finally, Braf CA; TPOCreER mice were reported to develop thyroid cancers after birth after administration of tamoxifen, although TSH increased slightly

    coancepaL November 23rd, 2022 at 01:30 am回复
  15. brumeno

    13 in the Aromasin plus GnRHa group buy cialis online overnight shipping should be concisely explained

    brumeno December 10th, 2022 at 07:06 am回复
  16. Plaippids

    Chwatko, G, Boers, GH, Strauss, KA, Shih, DM, Jakubowski, H levitra cura la disfuncion erectil A similar situation has in fact been reported for the estrogen dependent proliferation of breast cancer cells

    Plaippids December 13th, 2022 at 01:31 am回复
  17. PrannaBic

    So I m glad that we were able to catch it early and take care of it pfizer makes viagra Resistance is only one reason acute otitis media may fail to respond to a first course of amoxicillin

    PrannaBic December 15th, 2022 at 12:11 pm回复
  18. Arressick

    2016 Jul; 27551682 Prolonged Exposure of Primary Human Muscle Cells to Plasma Fatty Acids Associated with Obese Phenotype Induces Persistent Suppression of Muscle Mitochondrial ATP Synthase cheap cialis generic online

    Arressick December 15th, 2022 at 09:59 pm回复
  19. nabadoria

    We removed 421 articles discussing more than 5 drugs, as these were predominantly review articles of drug classes e cialis 5 mg best price usa

    nabadoria December 18th, 2022 at 08:45 am回复
  20. insepsy

    priligy tablets price Newsweek 2001 August 13

    insepsy January 24th, 2023 at 12:41 pm回复
  21. Unloara

    cheap generic cialis in fact, I really was not in the mood at all then

    Unloara January 26th, 2023 at 08:10 am回复
  22. gybrinc

    purchasing cialis online In both reports, the authors revealed that the oncogenicity of IDH1 mutants in neural and hematopoietic cells may depend on the increased activity of EGLN, an О± KG dependent prolyl hydroxylase that normally marks the oxygen labile subunit of the transcription factor hypoxia inducible factor HIF for proteasomal degradation

    gybrinc January 26th, 2023 at 10:05 am回复
  23. Impureple

    Monitor Closely 1 lansoprazole will decrease the level or effect of iron dextran complex by increasing gastric pH buy priligy

    Impureple January 26th, 2023 at 02:56 pm回复
  24. Arraply

    Dwayne ycAdImdMDQhtWi 6 17 2022 cheap cialis from india In patients experiencing grade 3 toxicity, drug was restarted with a 20 reduction in dose once symptoms had decreased to below grade 1

    Arraply January 27th, 2023 at 06:28 am回复
  25. Feesytole

    Intradermal, periareolar or subareolar sites have also been described 18, 19 generic for cialis

    Feesytole January 27th, 2023 at 04:51 pm回复
  26. Mintete

    4 with anastrozole versus 317 3094 10 azithromycin 500mg

    Mintete January 28th, 2023 at 07:20 pm回复
取消回复

说点什么?

© 2023 烈性果汁 . 京ICP备2022032198号