Jump to content

How do I check whether an object pointer is deleted?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
MerakSpielman

MerakSpielman

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Let's say I have a class called CAT.

I have a static int inside my CAT class to keep count of how many CATs exist.

I'm creating new objects kind of like this:

*Cat = CAT[64];
Cat[x] = new CAT;

x is determined elsewhere.

Now, I have two functions in my program. One, when called, will list the existing CATs. I call the static int inside CAT to know how many CAT pointers to list off. It works perfectly. It increments up when a CAT is made, so it always lists all the CATs I've created.

I have another function that lists the CATs and allows the user to choose one to delete:

delete cat[x];

easy enough.

But now my sequential list of CATS has a hole in it - the one that the user deleted. So when I try to run my other function, the one that simply lists my CATs, it crashes. It correctly knows the number of existing CATs, but not that one of the CAT pointers is now empty.

What I'm trying to figure out is a way of checking each CAT pointer to see if it's empty. If not, it continues checking the next one. If it is, it reassigns the remaining CAT pointers to be one number less. So instead of having
cat[0]
cat[1]
cat[3]
cat[4]

I'd have
cat[0]
cat[1]
cat[2]
cat[3]


The renumbering is easy, but what I'm missing is the command to test whether the pointer actually points to something, or if it has been deleted.
if (cat[x] = .......) what? null? 0? if (!cat[x])?

#2
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Make sure you are initiating the pointer to NULL if you want to do it that way.

Quote

if (cat[x] != NULL) {
}


#3
MerakSpielman

MerakSpielman

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
If I reset it to NULL, can I reassign that same pointer later?

#4
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Yes, it will just point to NULL until you set another address.

#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Yes, initialize to 0 and after you delete it, assign the pointer to 0.
delete cat[x];[COLOR="Blue"]

cat[x] = 0;[/COLOR]
[16] Freestore management, C++ FAQ Lite

#6
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
However only use delete if you are also using 'new'.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
You can also use the CRT debug assertions, like so:

#define _DEBUG //force debug version of code, remove in release version
#include <crtdbg.h>

if(_CrtIsValidHeapPointer(myPointer))
    printf("VALID.");
else
    printf("INVALID.");


However, you can't use it in release versions of your program, only if it's a debug version. When you remove the _DEBUG definition, the _Crt debug function calls are automatically removed for you.