Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 829973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:58:02+00:00 2026-05-15T03:58:02+00:00

On Learn C++, they wrote this to free memory: int *pnValue = new int;

  • 0

On Learn C++, they wrote this to free memory:

int *pnValue = new int; // dynamically allocate an integer
*pnValue = 7; // assign 7 to this integer
delete pnValue;
pnValue = 0;

My question is: “Is the last statement needed to free the memory correctly, completly?”

I thought that the pointer *pnValue was still on the stack and new doesn’t make any sense to the pointer. And if it is on the stack it will be cleaned up when the application leaves the scope (where the pointer is declared in), isn’t it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:58:03+00:00Added an answer on May 15, 2026 at 3:58 am

    Setting a pointer to NULL (or zero) after deleting it is not necessary. However it is good practice. For one thing, you won’t be able to access some random data if you later dereference the pointer. Additionally, you’ll often find code with the following:

    if(ptr)
    {
      delete ptr;
      ptr = NULL;
    }
    

    So setting the pointer to NULL will ensure it won’t be deleted twice.

    Finally, you might find code like this:

    void foo(bar *ptr)
    {
      if(!ptr) throw Exception(); // or return, or do some other error checking
      // ...
    }
    

    And you’d probably want that safety check to be hit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn how an application works. And for this I am
I'm beginning to learn javascript and I wrote an infinite loop <script> while(1==true){ document.write(hello
I am trying to learn inline assembly programming in Delphi, and to this end
Update: I wrote a working script that finishes this job in a reasonable length
I am trying to learn Asp.net Mvc so I am trying out this Tutorial.
I started to learn jQuery. I wrote a code, that will cycle the position
I am very new to Makefiles, so I am probably not doing this the
I want to learn a new programming language. I have in mind stuff like
I have seen the same question on this and many other websites but the
If this question is totally unsuitable, please forgive me. I don't know anything about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.