eliminate facebook

Deleting a pointer in C++

Deleting a pointer in C++

1 & 2

myVar = 8; //not dynamically allocated. Can’t call delete on it. myPointer = new int; //dynamically allocated, can call delete on it.

The first variable was allocated on the stack. You can call delete only on memory you allocated dynamically (on the heap) using the new operator.

3.

myPointer = NULL; delete myPointer;

The above did nothing at all. You didn’t free anything, as the pointer pointed at NULL.

The following shouldn’t be done:

myPointer = new int; myPointer = NULL; //leaked memory, no pointer to above int delete myPointer; //no point at all

You pointed it at NULL, leaving behind leaked memory (the new int you allocated). You should free the memory you were pointing at. There is no way to access that allocated new int anymore, hence memory leak.

The correct way:

myPointer = new int; delete myPointer; //freed memory myPointer = NULL; //pointed dangling ptr to NULL

The better way:

If you’re using C++, do not use raw pointers. Use smart pointers instead which can handle these things for you with little overhead. C++11 comes with several.


Deleting a pointer in C++

More Story on Source:

* Source→ *

171

Publish

👁️‍🗨️ PERCEIVE

अगर छुट्टी पर हैं तो संभलकर करें फेसबुक अपडेट {"_id":"420462d7e09468e07611cfb9fd61807d","slug":"be-careful-to-update-facebook-on-leave","type":"story","status":"publish","title_hn":"u0905u0917u0930 u091bu0941u091fu094du091fu0940 u092au0930 u0939u0948u0902 u0924u094b u0938u0902u092du0932u0915u0930 u0915u0930u0947u0902 u092bu0947u0938u092cu0941u0915 u0905u092au0921u0947u091f","category":{"title":"India News Archives","title_hn":"u0907u0902u0921u093fu092fu093e
میهن بلاگ - ابزار قدرتمند وبلاگ نویسی کاربران و همراهان سایت میهن بلاگ، سلام از اینکه مجبور هستیم از شما
Zuckerberge is Jealous ! China - TikTok ! Watch this . . .
Zoë Kravitz in Steven Soderbergh’s ‘Kimi’: Film Review Full disclosure — nothing makes me dread a review assignment right now
Zionist Political Violence: Patterns and Motives This attempt to tackle the issue of Zionist political violence will not constitute a
Zionism Contents What is Zionism? Theodor Herzl The Balfour Declaration Zionism and World War II Jewish Resettlement in Israel The
Zion woman claims she's being harrassed because she's Muslim: 'I just want them to stop' ZION, Ill. (WLS) -- This
YouTube Enhances Hatred of Israel and Extinguishes Hate for Palestinians Photo Credit: YouTube YouTube account suspension{Originally posted to the FirstOne
Your Rights When Losing or Leaving a Job Created by FindLaw's team of legal writers and editors | Last updated June
Your Official Instagram Hashtag Guide for Photographers: A List Of The Best Hashtags [Instagram Tips: Part 2] In Part 1 of

→ all Story at a.

Leave a Reply

Your email address will not be published. Required fields are marked *