Dab tsi yog qhov txawv ntawm rho tawm [] thiab rho tawm?
Dab tsi yog qhov txawv ntawm rho tawm [] thiab rho tawm?

Video: Dab tsi yog qhov txawv ntawm rho tawm [] thiab rho tawm?

Video: Dab tsi yog qhov txawv ntawm rho tawm [] thiab rho tawm?
Video: paj laim lauj mus ncig yuav khaub ncaws thaij nkauj 2024, Hlis ntuj nqeg
Anonim

Yog vim li cas thiaj muaj kev sib cais rho tawm thiab rho tawm operators yog li ntawd rho tawm hu onedestructor whereas rho tawm yuav tsum tau saib qhov loj ntawm thearray thiab hu hais tias ntau destructors. Lawm, siv ib qhov twg yuav tsum tau yuav ua rau muaj teeb meem.

Dhau li ntawd, qhov txawv ntawm free () thiab rho tawm yog dab tsi?

Qhov sib txawv ntawm kev rho tawm tus neeg ua haujlwm thiab dawb () muaj nuj nqi rho tawm yog tus neeg ua haujlwm ntawd dawb () yog alibrary muaj nuj nqi. rho tawm dawb lub cim xeeb faib thiab callsdestructor. Tab sis dawb () de-allocate nco tab sis tsis hu ua destructor. rho tawm yog sai dua dawb () vim tus neeg ua haujlwm ib txwm ceev tshaj qhov ua haujlwm.

Kuj Paub, rho tawm ua dab tsi hauv C ++? rho tawm () hauv C ++ Rho tawm yog tus neeg ua haujlwm ntawd yog siv destroy array thiab non-array(pointer) khoom uas yog tsim los ntawm kev qhia tshiab. Qhov twg txhais tau tias Rho tawm operatordeallocates nco los ntawm heap. Pointer rau qhov khoom yog tsis raug puas tsuaj, tus nqi lossis kev nco thaiv tau taw qhia los ntawm tus taw tes yog puas lawm.

Ntawm no, qhov txawv ntawm tshem tawm thiab rho tawm yog dab tsi?

Rho tawm thiab tshem tawm txhais tau zoo ib yam, tab sis lub ntsiab qhov sib txawv ntawm lawv yog li ntawd rho tawm txhais tau tias lwv (piv txwv li ua tsis muaj nyob lossis tsis tuaj yeem rov qab tau), thaum tshem tawm connotes tshem tawm thiab setaside (tab sis khaws cia hauv hav zoov).

Yuav ua li cas destructor txawv ntawm tus neeg teb xov tooj rho tawm?

Thawj tus rhuav tshem , hu ua cov khoom tiav tus rhuav tshem , ua kev puas tsuaj yam tsis tau hu rho tawm () ntawm qhov khoom. Qhov thib ob tus rhuav tshem , npe tshem tawm destructor , hu rho tawm () tom qab rhuav tshem qhov khoom. Yog ib yam khoom puas tab sis tsis tshem tawm los ntawm lub pob, hu tus neeg teb xov tooj rho tawm yuam kev.

Pom zoo: