0

I come from Java side where if you want to check if a particular entry in an ArrayList is allocated or not, you get the reference of the entity in ArrayList and check to see if it is null or not.

Basically I am trying to accomplish the same thing. Here is a simplified version of my code:

struct node
{
   int count=0;
   int nodeval=0;

   bool valid=false;
};

bool operator==(const node* n,int val)
{
   if (n->count==0&&n->nodeval==0&&n->valid==false)
   if(val==NULL)
      return true;
   else
      return false;
}

node* lookup(int val)
{
   return &Table[val];
}

int main()
{
   res=somevalue;
   node* child=lookup(res);
   if(child==NULL)
   {
      //do something
   }
}

Here is my understanding:

  1. lookup() is going to return me the address of a select position in Table data structure. I store that in child.
  2. Checking child for null pointer didn't make sense because we return a proper reference of a memory location so I overload it.
  3. In the overload ==, I check if each of the fields is unaltered, if it is so it is not changed and hence a 'null entity'. The problem is that == operator requires one user-defined object to operate upon.

Questions:

  1. What did I get wrong in fore-mentioned steps?
  2. Is there an easier way to check if an object pointed to by a pointer isn't altered/null?
9
  • 1
    Why are you overloading operators? Just check if the pointer is NULL or not. There is no need for operator overloading. Also -- C++ is not Java. Don't try and emulate Java code or techniques in C++. Commented Aug 13, 2015 at 4:03
  • 1
    @PaulMcKenzie,, when I do &Table[val] I am referring to the location (Table 0+val) address and that happens to be an allocated memory and I return it,, now how is this going to help me to check for null? Commented Aug 13, 2015 at 4:08
  • 3
    @Galik If you really want to do this the Java way And thus the reason why I stated to the OP that C++ is not Java. Emulating Java in C++ leads to code that is either hard to understand, or just plain wrong / weird. Commented Aug 13, 2015 at 4:16
  • 2
    Objects aren't null. Pointers are null, or point to objects. They don't point at null objects. Your question doesn't make sense. Commented Aug 13, 2015 at 4:40
  • 1
    C++ does not have null objects. Nor does it have object references like Java. (Some will try and tell you that C++ pointers or references are like Java object references, but they are wrong). Commented Aug 13, 2015 at 4:48

1 Answer 1

1

It's not 100% clear what your question is, but I'll try to clarify a few misconceptions I see in your code:

  1. lookup() can return null. The function is declared to return a pointer, which can be null. If this isn't an error (see below), just return null.

  2. Inside lookup(), you first need to check whether Table actually contains an element with that index. How to do that depends on the type of Table. If it's a plain array, just check if the index is less than the size in elements and return null if not.

Note that if you always expect a result from lookup, the returned type should be a reference instead of a pointer. In that case though, you need error handling for invalid indices. You could declare those as programming error and just assert() that they are valid. Alternatively, you could throw std::out_of_range("invalid index").

Lastly, concerning your question "How to check if an object pointed to by a pointer is null?": The answer is that if you have a pointer Object* ptr, you just dereference the pointer *ptr and compare that to null (i.e. if (*ptr == 0)). However, that requires that the object's type is comparable to null, which is typically only the case for numeric objects (bool, char, int, float etc) and pointers. Objects of your node class typically are not comparable to null and it doesn't make much sense to make them comparable to null (using operator overloading) either.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.