Operator New and Delete - Unnecessary Conditions

19:39
Fri
09
Dec 2016

Operator New and Delete - Unnecessary Conditions

I've seen following constructs many times in C++ code:

MyClass* obj = new MyClass();
if(obj == nullptr)
    // Handle allocation error.

// ...

if(obj != nullptr)
    delete obj;

Both of these conditions are unnecessary. Strictly speaking they are not a bug - program will run correctly, but they make no sense. If you used to write any of these, you should know that:

1. Operator new doesn't return null on failed allocation. By default it throws an exception of type std::bad_alloc in this case, so this is what you should handle if you really care about the state of your program after it runs out of memory (or if the allocated object is particularly big).

There is also special nothrow version of the new operator that returns null on failure, but you must call it explicitly. An alternative would be to overload new operator (global or for particular class) to change its behavior. But again, this is not what happens by default.

Note this is different behavior than malloc function from C. Obviously there are no exceptions in C language, so this function just returns null on failure.

2. Operator delete doesn't crash when you pass null pointer to it - it just does nothing, so the check for not-null is already inside, you don't have to write it. Of course trying to delete object from any non-null address that was already freed or is just invalid address still crashes, or causes other undefined behavior.

Note this is the same behavior as free function from C - it also accepts null pointer.

Comments (1) | Tags: c++ | Author: Adam Sawicki | Share

Comments

ylq
2017-02-22 09:23:09
false http://www.nike-max.fr/ happiness, http://www.ed-hardy.us.com/ but http://knicks.nba-jersey.com/ to http://www.michaelkorsbags.us.org/ see http://www.woolrich-clearance.com/ him http://www.newbalanceshoes.com.es/ (her) http://www.katespadeoutlet.gb.net/ that http://www.tomsshoes-outlet.us.com/ happiness http://kings.nba-jersey.com/ is http://www.uggs-onsale.net/ really http://www.cheapjerseys.net.co/ indescribable http://ravens.nfljersey.us.com/ bitterness. http://www.montblancpens-sale.com/ But, http://www.tory-burch.us.org/ those http://www.calvin-kleins.net/ first http://www.pradaoutlet.com.co/ so-called http://www.oakley-outlet.net.co/ naive, http://celine.blackfriday.in.net/ will http://pistons.nba-jersey.com/ become http://www.supra-shoes.org/ mature http://www.michael-kors-taschen.com.de/ in http://www.cheap-thomassabos.co.uk/ the http://www.nfl-jersey.us.org/ dust, http://michaelkors.blackfriday.in.net/ sinking http://www.cheap-rayban.com.co/ pure http://www.swarovski-australia.com.au/ heart, http://www.oakleys-outlet.in.net/ will http://azcardinals.nfljersey.us.com/ eventually http://timberwolves.nba-jersey.com/ be http://www.abercrombie-kid.us.com/ in http://www.coach-factoryyoutletonline.net/ the http://www.converse.net.co/ years http://www.salvatoreferragamo.in.net/ to http://redskins.nfljersey.us.com/ bleach http://www.ray-banssunglasses.co.uk/ the http://www.tommyhilfigerca.ca/ vicissitudes http://colts.nfljersey.us.com/ of http://www.retro-jordans.com/ appearance, http://www.beats-by-dre.com.co/ then, http://www.christian-louboutinshoes.in.net/ that http://www.rayban-pas-cher.fr/ a http://www.abercrombie-andfitchs.com/ simple http://www.wedding-dresses.cc/ smile, http://www.nikeair-max.es/ a http://www.mcm-handbags.org/ little http://www.ghdhairstraightener.cc/ happiness, http://www.philipp-plein.us.com/ hugs http://www.thenorthface.com.de/ have http://www.ralphlaurenonlineshop.de/ become http://ralphlauren.blackfriday.in.net/ wanton http://www.longchamp.com.de/ the http://www.ralphs-laurens.co.uk/ dusty http://www.timberlandbootsoutlet.us.com/ memories, http://www.michaelkors.com.se/ and http://www.burberry-outletonline.cc/ that http://michaelkors.euro-us.net/ the http://www.burberry-outletcanada.ca/ road http://www.insanity-workout.us.com/ behind http://www.ugg-boots-australia.com.au/ us http://76ers.nba-jersey.com/ is http://www.northfaceoutlet.com.co/ called http://www.pandorajewelry.top/ to http://www.coach-factory.in.net/ grow!Look http://www.nike-rosheruns.nl/ up http://www.ralph-laurenpolosoutlet.com/ the http://www.oakleyoutlet.ar.com/ tide http://www.pandorajewellery.com.au/ will http://www.uggs.co.nl/ still http://www.mizuno-running.net/ be http://www.marc-jacobsonsale.com/ returned, http://www.polos-outlets.com/ it http://www.truereligions.net/ still http://www.burberry-outletsonline.org.uk/ was http://www.uggsstore.us.com/ the http://www.omegawatches.in.net/ flow http://www.nike-schuhe.com.de/ of http://coach.misblackfriday.com/ the http://www.oakley-sunglasses.mex.com/ appearance, http://www.michaelkors.so/ but http://www.canadagoosesonline.com/ some http://www.prada-handbags.net.co/ things, http://www.coachblackfriday.com/ twist http://www.oakleys-2017.in.net/ once, http://www.new-balance-schuhe.de/ it http://www.ugg-australia.com.de/ is http://grizzlies.nba-jersey.com/ hard http://www.burberryoutlet-sale.net/ to http://www.hollisters.us.com/ restore http://www.nike-air-force.de/ its http://www.pandoras-charms.org.uk/ original http://www.uggs-boots.net.co/ appearance, http://www.michael-kors-australia.com.au/ for http://www.nikefree5.net/ example http://www.p90xworkout.in.net/ - http://www.rayban.co.nl/ love. http://www.monclers-outlet.us.com/ Perhaps, http://www.hollister-clothing.in.net/ very http://www.oakleys-outlet.it/ often, http://www.ralphlauren-au.com/ it http://www.rolex-watches.us.com/ is http://rams.nfljersey.us.com/ that http://heat.nba-jersey.com/ we http://buccaneers.nfljersey.us.com/ expect http://www.uhren-shop.com.de/ too http://www.truereligionjeans.net.co/ much, http://www.burberryoutlets.net.co/ so http://www.scarpe-hoganoutlets.it/ much http://www.oakleys-sunglasses.in.net/ disappointed, http://www.coachoutlet-online.com.co/ because http://www.air-maxschoenen.nl/ understand, http://www.nfljersey.us.com/ no http://www.airmax-90.org/ longer http://www.asics-shoesoutlet.com/ hysterical, http://www.underarmour.us.com/ gradually http://www.co-aol.com/ become http://www.newbalance-outlet.org/ quiet. http://www.nba-jersey.com/ Over http://www.replica-handbags.net.co/ time, http://www.tommy-hilfiger.in.net/ the http://www.nike-mercurial.in.net/ body http://www.michaelkors-outletonline.cc/ will http://pacers.nba-jersey.com/ unconsciously http://www.burberrys-outlets.org.uk/ give http://lakers.nba-jersey.com/ birth http://www.coach-outletonline.net.co/ to http://www.nikeairmaxnc.co.uk/ something http://www.cheap-nike-shoes.net/ called http://www.burberry-handbagssale.com.co/ cautious http://www.poloralphlauren.cc/ antibody.The http://www.cheapjerseys.us.org/ best http://www.cheap-baseballbats.us/ love http://www.swarovski-canada.ca/ can http://nets.nba-jersey.com/ stand http://uggboots.misblackfriday.com/ the http://www.jordanretro.org/ time http://www.vans-schuhe.com.de/ of http://www.nikerosherun.us/ grinding, http://www.christian-louboutins.in.net/ the http://www.nikestore.us/ test http://celtics.nba-jersey.com/ stand http://www.michaeljordan.com.de/ the http://www.lauren-ralphsoutlet.co.uk/ distance, http://www.nikeshoesoutlet.org.uk/ you http://www.kates-spade.com/ can http://magic.nba-jersey.com/ instant http://www.bcbg-maxazria.ca/ sensation, http://www.uggs-boots.com.co/ can http://www.nike-huaraches.nl/ also http://www.christianlouboutin.org.uk/ be http://www.mcmbackpacks.in.net/ attributed http://lions.nfljersey.us.com/ to http://www.michaelkorsoutlet-online.ar.com/ flat http://www.jimmy-chooshoes.com/ bloom http://www.nikeair-max.ca/ again http://www.oakleyssunglasses.in.net/ when http://www.soccer-shoesoutlet.com/ the http://www.mbt-shoes.us.com/ wedding http://patriots.nfljersey.us.com/ day. http://www.juicycoutureoutlet.net.co/ Two http://www.toms-shoes.net.co/ people http://www.nike-skor.com.se/ together, http://coach-outlet.tumblr.com/ often http://www.hogan.com.de/ do http://www.newbalance-shoes.org/ not http://broncos.nfljersey.us.com/ know http://www.hermes-outlet.net.co/ how http://www.hollisterclothingstore.org/ precious http://www.michael-korsoutlet.cc/ love, http://www.raybans-outlet.com.co/ thinking http://saints.nfljersey.us.com/ that http://www.nike-roshe-run.de/ it http://www.jordanrelease-dates.us.com/ is http://www.beatsbydre.com.co/ just http://www.ralph-laurens.org.uk/ a http://www.toms-outlet.net/ normal http://www.michael-korshandbags.us.org/ life, http://bengals.nfljersey.us.com/ although http://www.airmax-2015.org/ sweet, http://www.mk-com.com/ but http://www.adidas-shoes.cc/ bland, http://www.burberryonlineshop.de/ but http://49ers.nfljersey.us.com/ once http://www.cheapuggs-boots.com.co/ separated, http://www.ferragamo.com.co/ the http://falcons.nfljersey.us.com/ kind http://www.givenchy.in.net/ of http://www.airyeezy.us.com/ feeling http://www.bottega-veneta.in.net/ one's http://www.hermesbirkin-outlet.com/ heart http://www.adidasshoes.org.es/ is http://www.abercrombie-andfitch.ca/ cut http://www.true-religions.com/ off, http://www.raybans-outlet.cc/ it http://www.juicycouture.com.co/ will http://www.barbour.in.net/ let http://www.versaceoutlet.us.com/ you http://www.truereligion-outlet.us.org/ know http://www.raybanoutlet.ca/ the http://www.barbour-factory.com/ original http://www.nike-rosherun.com.es/ plain http://www.airjordans.us/ life http://www.giuseppezanotti.com.co/ is http://www.designerhandbagsoutlet.net.co/ the http://www.longchampoutlet.com.co/ greatest http://www.vibram-fivefingers.in.net/ gift http://www.christianlouboutinshoesoutlet.org/ of http://clippers.nba-jersey.com/ love, http://nuggets.nba-jersey.com/ because http://www.iphone-cases.in.net/ love http://jazz.nba-jersey.com/ was http://www.michael-korssale.us.com/ in http://www.the-northfacejackets.co.uk/ the http://cowboys.nfljersey.us.com/ flat http://www.mcms-handbags.com/ and http://www.uggs-online.com/ show http://www.nikeair--max.fr/ its http://www.pradahandbags.net.co/ real http://www.converse-shoes.net/ long. http://jaguars.nfljersey.us.com/ I http://www.tommy-hilfigers.com/ thought, http://www.tommy-hilfiger-online.de/ only http://www.basketballshoes.com.co/ broke http://giants.nfljersey.us.com/ our http://www.jimmy-choos.com/ hearts http://eagles.nfljersey.us.com/ to http://www.polos-ralphlauren.us.org/ prove http://www.salomon-schuhe.com.de/ that http://coachoutlet.euro-us.net/ the http://www.giuseppe-zanotti.net/ feeling http://www.cheap-mlbjerseys.us.com/ of http://www.barbour-jackets.us.com/ love http://www.burberryoutlet-sale.com.co/ came http://bears.nfljersey.us.com/ later, http://thunder.nba-jersey.com/ beginning http://www.ugg-boots.us.org/ to http://www.ralph-laurenoutletonline.in.net/ understand, http://www.burberry-handbagsoutlet.net.co/ that http://www.guessfatory.net/ sweet http://www.nikefreeshoes-inc.co.uk/ feeling http://www.monclerjackets.in.net/ of http://www.prada-shoes.com.co/ love http://www.nikeskoes.dk/ is http://www.oakleyoutlet.fr/ the http://www.longchamp-bags.us.com/ traces http://www.omega-watches.com.co/ of http://www.michael-kors-outlet.us.org/ the http://browns.nfljersey.us.com/ painful http://chiefs.nfljersey.us.com/ feeling http://suns.nba-jersey.com/ just http://www.burberrybags-sale.net/ as http://panthers.nfljersey.us.com/ reconciled. http://rayban.blackfriday.in.net/ If http://www.uggsoutlet.com.co/ you http://www.abercrombiehollister.nl/ can, http://www.pulseraspandora.com.es/ in http://www.ray-banssunglasses.org/ this http://texans.nfljersey.us.com/ life, http://www.ralphlaurens-outlet.co.uk/ to http://www.adidas-superstars.nl/ be http://www.horlogesrolex.nl/ a http://www.mk-outletonline.us/ gentle http://www.nike-outlet.us.org/ woman, http://www.abercrombie-and-fitch.us.com/ no http://www.tomsoutlet-online.net/ longer http://wizards.nba-jersey.com/ begging http://bulls.nba-jersey.com/ for http://bucks.nba-jersey.com/ love, http://vikings.nfljersey.us.com/ do http://packers.nfljersey.us.com/ not http://www.hugo-bossoutlet.com/ dust http://www.hollisteronlineshop.com.de/ the http://www.polo-ralph-lauren.de/ flowers, http://www.converse.com.de/ offer http://www.oakleys.org.es/ the http://www.adidassuper-star.de/ promenade http://www.raybans.org.es/ years, http://www.toms-shoesoutlet.us/ we http://titans.nfljersey.us.com/ found http://www.the-northfacejackets.net.co/ an http://coach.euro-us.net/ ordinary, http://www.converses-outlet.com/ simple http://www.hollisters-canada.ca/ love, http://www.kate-spades.com.co/ if http://airmax.misblackfriday.com/ this http://www.thenorthfacejackets.fr/ desire http://www.nikestore.com.de/ can http://www.timberland-boots.com.co/ be http://www.burberrys-bags.net.co/ a http://www.zxcoachoutlet.com/ little http://www.nikefree-run.org.uk/ luxury, http://www.nike-factorys.us/ I http://www.omegarelojes.es/ want http://www.to-coachoutlet.com/ it http://jets.nfljersey.us.com/ intact.[For http://www.prada.com.de/ years] http://www.tnf-jackets.us.com/ streamerIn http://www.thenorth-face.ca/ this http://www.fendi-outlet.in.net/ romantic http://www.nike-tnrequinpascher.fr/ turn http://www.hollister-abercrombie.com.se/ clouds, http://www.rosheruns.us/ dust-free http://www.ralph-laurenoutletonline.com/ season http://www.cheapmichaelkors.us.org/ in http://www.ray-bansoutlet.in.net/ Autumn, http://www.air-max.com.de/ I http://www.tory-burchoutlet.com.co/ can http://raiders.nfljersey.us.com/ not http://www.cheapreplica-watches.in.net/ do http://www.adidasshoesca.ca/ forgive http://www.ugg-bootscanada.ca/ bright http://www.michael-kors.net.co/ as http://www.christianlouboutin-shoes.ca/ the http://www.toms--outlet.com.co/ sun, http://www.true-religion.com.co/ happy, http://www.ralphslaurenoutlet.us.com/ smiling, http://www.cheap-rolex-watches.org.uk/ forgive http://www.dsquared2.us.com/ me http://www.nike-air-max.com.au/ so http://www.oakley-outletonline.com.co/ quiet http://www.uggsoutlet.net.co/ silent, http://www.cheap-jerseys.mex.com/ alienated http://www.puma-shoes.de/ deserted. http://www.raybans-sunglass.com/ I http://www.cheapnhljerseys.us.com/ just http://www.vans-shoes.co.uk/ want http://www.michael-kors.cc/ to http://www.puma-shoesoutlet.com/ leaning http://www.bcbg-dresses.com/ against http://www.newbalancecanada.ca/ the http://bills.nfljersey.us.com/ window, http://www.rosherun.co.uk/ watching http://www.uggboots.com.de/ a http://www.toms-shoe.us.com/ falling http://www.ray-bans.net.co/ Raindance, http://www.michael-korsbags.co.uk/ read http://www.rayban.com.de/ a http://www.poloralphlaurenoutlet.net.co/ chapter http://www.pandora.com.de/ Que http://www.michaelkors-canadaoutlet.ca/ warm http://www.cheap-michaelkors.com/ words, http://www.timberlandshoes.net.co/ the http://www.rolexwatchesforsale.us.com/ only http://www.adidas-schuheonline.de/ way http://www.nikeshoes.org.es/ I http://trailblazers.nba-jersey.com/ will http://www.soccers-shoes.net/ not http://www.swarovski-online-shop.de/ bored http://dolphins.nfljersey.us.com/ of http://hawks.nba-jersey.com/ Language http://www.polos-outletstore.net/ and http://www.tommyhilfiger.net.co/ such http://www.barbour-jacketsoutlet.net/ incompetent http://www.coach-outletonline.ca/ and http://www.valentino-shoesoutlet.us/ weak http://longchamp.blackfriday.in.net/ situation, http://www.soft-ballbats.com/ sorrow. http://coach.blackfriday.in.net/ In http://www.tommy-hilfiger.com.de/ fact, http://www.the-northfaces.us.com/ I http://www.swarovskijewelrys.in.net/ just http://pelicans.nba-jersey.com/ do http://www.bottega-venetas.cc/ not http://www.hermes-bags.net/ want http://www.womenclothes.in.net/ people http://www.mcmhandbags.com.co/ to http://www.iphone-cases.net/ visit, http://www.rayban-pascher.fr/ do http://www.truereligion-outlet.com.co/ not http://www.adidas.us.com/ want http://www.reebok.com.de/ to http://www.cheap-michaelkors.in.net/ be http://michaelkors.misblackfriday.com/ sympathy, http://www.adidas.com.se/ just http://www.rayban-sunglasses.co/ want http://www.chiflatiron.net.co/ a http://www.celine-bags.org/ quiet http://www.asicsoutlet.us.org/ daze http://www.lacosteoutlet.us.com/ Ye http://www.instylers.us.org/ Hao, http://www.ok-em.com/ Ye http://www.nikefree-run.net/ silent, http://www.longchamp.com.co/ scattered http://www.tory-burchsandals.in.net/ or.There http://www.michaelkorsoutletonline.net.co/ is http://www.pandoracharms-canada.ca/ always http://www.thomas-sabo.com.de/ a http://www.uggs-australia.com.co/ period http://www.timberlands-paschere.fr/ of http://www.oakleysunglasses-canada.ca/ time, http://www.michaelkors.co.nl/ I http://www.nikeshoes-outlet.com/ became http://www.sunglasses-outlet.net/ very http://www.cheap-jerseys.cc/ indifferent, http://www.jordan-shoes.com.co/ did http://seahawks.nfljersey.us.com/ not http://www.oakleys2016.com/ want http://www.the-northfaces.net.co/ to http://www.katespades-outlet.in.net/ talk http://www.mlb-jerseys.us.com/ to http://www.coach-factoryoutlet.net.co/ anyone, http://www.christianlouboutinshoes.jp.net/ do http://www.ferragamoshoes.in.net/ not http://www.nike-air-max.com.se/ want http://www.ralph-laurenspolo.co.uk/ to http://www.raybanssunglasses.in.net/ send http://www.cheap-oakleyssunglasses.com/ text http://www.rolex-watches.net.co/ messages http://www.cheap-raybansoutlet.in.net/ to http://raptors.nba-jersey.com/ anyone, http://www.the-northface.com.co/ and http://www.canadagooses-jackets.com/ do http://www.burberry-outletstore.net/ not http://www.chi-flatiron.us.com/ want http://www.long-champoutlet.com/ to http://www.newoutletonlinemall.com/ contact http://www.ralphlauren-polos.com.co/ anyone, http://www.ray-bansoutlet.it/ just http://www.designer-handbagsoutlet.us.com/ buried http://www.mcm-bags.us.org/ himself http://www.nike-airmax.us.com/

http://mavericks.nba-jersey.com/ http://www.vans-shoes.net/ http://www.nhl-jerseys.us.com/ http://cavaliers.nba-jersey.com/ http://www.oakleys.in.net/ http://www.air-huaracheshoes.co.uk/ http://www.beatsheadphone.in.net/ http://www.oakley.com.de/ http://steelers.nfljersey.us.com/ http://www.michaelkors-bags.com.co/ http://www.michael-kors.com.es/ http://www.oakleys-frame.net/ http://warriors.nba-jersey.com/ http://www.toms-outlets.us.com/ http://www.cheap-jordans.net/ http://www.outlet-burberry.net.co/ http://www.swarovski-crystals.us.com/ http://hornets.nba-jersey.com/ http://www.abercrombiefitchs.cc/ http://www.longchamps.us.com/ http://www.ray-bans.co.uk/ http://www.ugg-bootsclearance.com/ http://www.nike-air-max.com.de/ http://www.barbours.us.com/ http://www.oakley--sunglasses.com.au/ http://oakley.blackfriday.in.net/ http://www.michaelkorsoutlet.ar.com/ http://www.nba-shoes.com/ http://www.hollister-clothingstore.com/ http://www.levisjeans.com.co/ http://www.marc-jacobs.us.com/ http://www.handbagsoutlet.in.net/ http://www.toryburchsale.com.co/ http://www.tracksuits-store.com/ http://www.chrome-hearts.in.net/ http://www.armani-exchange.in.net/ http://www.beatsbydrdrephone.com/ http://www.michael-korsbags.org.uk/ http://www.swarovskissale.co.uk/ http://www.nike-free-run.de/ http://www.nike-schoenen.co.nl/ http://www.nike-shoescanada.ca/ http://www.babylisspros.in.net/ http://www.rolexwatches-canada.ca/ http://www.montres-pascher.fr/ http://chargers.nfljersey.us.com/ http://www.ralphlaurencanada.ca/ http://spurs.nba-jersey.com/ http://www.tommy-hilfiger.co.nl/ http://www.christianlouboutinoutlet.net.co/ http://www.cheapshoes.net.co/ http://www.oakleysunglasses2017.com/ http://rockets.nba-jersey.com/ http://www.longchamp-handbagsoutlet.us.com/ http://www.air-huarache.co.uk/

Post comment

Nick *
Your name or nickname
E-mail
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki