Pointers Question.....Null Pointers!!!!

This is a discussion on Pointers Question.....Null Pointers!!!! within the C++ Programming forums, part of the General Programming Boards category; My question is after you delete a pointer and then set it to null.....Can you assing a value to this ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Pointers Question.....Null Pointers!!!!

    My question is after you delete a pointer and then set it to null.....Can you assing a value to this pointer after you've set it to null?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Unregistered
    Guest
    After you delete a pointer, you cannot assign any value to it. If you meant create it using new, then yes you can reassign its value. Pointers can be reassigned while references cannot.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    After you delete a pointer it's gone, you can't use it anymore. However, if you intend to use it again then you can set it to NULL when you've finished with it's original value so that you don't accidentally change that value and then set it to the new value when you get to that point in your program.

    As a matter of good practice you should set all pointers to NULL when you create them unless you immediately set their value. And when you're done using them but don't want to destroy them completely you set them to NULL for safety and debugging reasons.
    C code. C code run. Run code, run...please!

  4. #4
    Unregistered
    Guest
    int * ptr;

    ptr doesn't point to any spot in memory in particular yet, but rather is pointing to some random spot in memory. You could to this:

    cout << &ptr << endl;

    to see what address ptr is found at. But if you try this right now:

    cout << *ptr << endl;

    you never know what is going to happen. To fix this you need to point ptr at some spot in memory before you try to change it's value or do something with it besides using it's own address. This is one way to do that.

    int num = 8;

    ptr = &num;

    ptr is now pointing to the spot in memory that was assigned to the variable num, it is no longer random. Now you can do this:

    cout << *ptr << endl;

    and know what to expect. If you want to point ptr someplace but not at some thing then you can do this:

    ptr = NULL;

    ptr is still real but it is pointing to a very special place in memory that doesn't represent anything. If you do this:

    cout << &ptr << endl;

    you will see that ptr is still at the same spot in memory. But if you do this:

    cout << *ptr << endl;

    you should get an error message because NULL is not a value, just a place.

    You can also assign a spot in memory to ptr by using the new operator:

    ptr = new int;

    If you do this now:

    cout << &ptr << endl;

    you should still get the same result as above. If you try:

    cout << *ptr << endl;

    right now you should get an error or crash your program if you are lucky, because even though you have assigned a spot in memory to ptr, the spot in memory still doesn't have a value. Now the only way you can reach the spot in memory pointed to by ptr is to use ptr itself. Therefore you need to do something like this:

    *ptr = 221;

    and then you can do this;

    cout << *ptr <<endl;

    So far pretty basic pointer schtuff. Now let's say you don't need the memory assigned to ptr anymore so you want to delete it. No problem, do this:

    delete ptr;

    UNFORTUNATELY the above line doesn't delete ptr itself, it deletes the memory ptr points to. You can still do this:

    cout << &ptr << endl;

    and it should still be the same as it was every other time. But now ptr doesn't point to any memory and is therefore pointing someplace random again. So you can fix that by doing something like:

    ptr = NULL;

    or whatever. Remember the pointer itself is not deleted by delete, the memory the pointer points to is what is deleted. The pointer remains and can be used for any valid purpose after the delete statement.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    Oh dear CeeCee, now I have to change my quote

    I didn't really read this post that much, so if what I say has already been said sorry. If by deleting a pointer you mean freeing the memory that it points to then yes, you can reassign the pointer. You can reassign the pointer even if you don't "delete" it first, though this is a good way to create a memory leak. You must know though that you are not actually deleting the pointer, you are freeing the memory that it points to. One problem that I see with learning C++ before one learns C is that most C++ books don't explain what a pointer is and how it is different from an array or reference variable for that matter. That is how little confusions like this occur. Anyways C++ isn't really my thing, but I know a little, hope this helped...
    one fish two fish
    red fish blue fish

  6. #6
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    You could to this:

    cout << &ptr << endl;

    to see what address ptr is found at. But if you try this right now:

    cout << *ptr << endl;

    you never know what is going to happen
    Assigning a pointer to an unknown chunk of memory is a good way to get a random value, especially in this case, where you'd get a random number without having to use formal functions.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Total newb directx invisable geometry question
    By -pete- in forum Game Programming
    Replies: 5
    Last Post: 08-13-2006, 01:45 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  5. Problem with a menu.
    By Rare177 in forum Windows Programming
    Replies: 4
    Last Post: 09-07-2004, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21