Thread: Create a null pointer after using a delete

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61

    Create a null pointer after using a delete

    In jumping into C++ it says something like this: It's not necessary but when you delete a pointer it's a good idea to reset it as a null pointer. That if your code try's to dereference the pointer after being freed, your program will crash. This happens to a lot of experienced programmers This could corrupt users data.
    delete p_int;
    p_int = NULL;

    This raises a lot of questions for me as a beginners.

    1. If you can deference a pointer after the memory is freed, why can't you just delete the pointer?

    2. If you can do 1, how do you delete the pointer using code?

    3. Every thing I've read says that free memory is handed out in a sequenced order. I don't believe that is true at all. I may be wrong. Why can't you put the data in any number of places if it will fit. Isn't the compiler smart enough to know where bytes (bits)and pieces are stored?

    4. If you storing anything in free memory must use a pointer to it?

    5. Can a pointer or something similar be used with stack memory?




    Last edited by papagym177; 08-07-2014 at 05:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  2. delete NULL;
    By Just in forum C++ Programming
    Replies: 10
    Last Post: 11-25-2004, 07:07 PM
  3. delete on a NULL pointer
    By myname in forum C++ Programming
    Replies: 18
    Last Post: 10-03-2003, 08:23 PM
  4. Which comes first...delete [] or NULL?
    By Waldo2k2 in forum C++ Programming
    Replies: 13
    Last Post: 08-09-2002, 09:05 AM