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

  • Home
  • SEARCH
  • 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 8777253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:14:14+00:00 2026-06-13T19:14:14+00:00

Context: I’m trying to wrap my head around pointers, we just saw them a

  • 0

Context: I’m trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience.

I’ve seen quite a few questions over in SO about deleting pointers but they all seem to be related to deleting a class and not a ‘simple’ pointer (or whatever the proper term might be), here’s the code I’m trying to run:

#include <iostream>;

using namespace std;

int main() {
  int myVar,
      *myPointer;

  myVar = 8;
  myPointer = &myVar;

  cout << "delete-ing pointers " << endl;
  cout << "Memory address: " << myPointer << endl;

  // Seems I can't *just* delete it, as it triggers an error 
  delete myPointer;
  cout << "myPointer: " << myPointer << endl;
  // Error: a.out(14399) malloc: *** error for object 0x7fff61e537f4:
  // pointer being freed was not allocated
  // *** set a breakpoint in malloc_error_break to debug
  // Abort trap: 6

  // Using the new keyword befor deleting it works, but
  // does it really frees up the space? 
  myPointer = new int;
  delete myPointer;
  cout << "myPointer: " << myPointer << endl;
  // myPointer continues to store a memory address.

  // Using NULL before deleting it, seems to work. 
  myPointer = NULL;
  delete myPointer;
  cout << "myPointer: " << myPointer << endl;
  // myPointer returns 0.

}

So my questions are:

  1. Why won’t the first case work? Seems the most straightforward use to use and delete a pointer? The error says the memory wasn’t allocated but ‘cout’ returned an address.
  2. On the second example the error is not being triggered but doing a cout of the value of myPointer still returns a memory address?
  3. Does #3 really work? Seems to work to me, the pointer is no longer storing an address, is this the proper way to delete a pointer?

Sorry for the long question, wanted to make this as clear as possible, also to reiterate, I have little programming experience, so if someone could answer this using layman’s terms, it would be greatly appreciated!

  • 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-06-13T19:14:15+00:00Added an answer on June 13, 2026 at 7:14 pm

    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.

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

Sidebar

Related Questions

Context I am trying to understand the internals of a big, hairy JavaScript library.
Context I need to warn my website's users of events by sending them an
Context: I have some data, and I wish to: plot a histogram of them
Context I'm learning C, and I'm trying to reverse a string in place using
Context : I am trying to put up a Decorator pattern in Ruby. As
Context I've just installed postgres 9.1.4 on Lion, and i would like to write
Context: I'm a just-hours-old newbie at OpenLayers, please be gentle. Fundamentally, I have a
Context: Windows7 64bit, ActiveDirectory, Windows Server 2003 I'm trying to get the code given
Context Say, we want to use the Box-Muller algorithm. Starting from a couple of
Context: the application I'm trying to make does not display a form initially, but

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.