• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

Assert(lst != NULL) --or-- Assert(*lst != NULL) ?

Hi,

I have a function which receives a pointer to a linked list. Something like:

void func(LinkedList * lst)

I want to make sure the lst isn't null.
What should I check then?

Assert(lst != NULL);
  or
Assert(*lst != NULL);
  or both, i.e.
Assert((lst != NULL) && (*lst != NULL));

Thanks!
0
slavikn
Asked:
slavikn
  • 4
  • 4
1 Solution
 
sunnycoderCommented:
Hi slavikn,

 Assert(lst != NULL);

Sunnycoder
0
 
sunnycoderCommented:
>Assert(lst != NULL);
Make sure that we have not been passed a NULL pointer and it points to some memory location

>Assert(*lst != NULL);
Make sure that the value held at memory location pointed to by lst is not NULL ... if lst is NULL, then *lst may cause segfault
0
 
slaviknAuthor Commented:
Hmm... from what you wrote in your 2nd post, it seems that I should check both:
Assert((lst != NULL) && (*lst != NULL));
so that I know the pointer isn't NULL, and the pointed object isn't NULL, right?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sunnycoderCommented:
>so that I know the pointer isn't NULL, and the pointed object isn't NULL, right?
That is exactly what checking both would mean
0
 
slaviknAuthor Commented:
Ok. I just wanted to be sure I was right. Thanks.
0
 
slaviknAuthor Commented:
Oops...

int DbIsEmpty(DB * db)
{
   MyAssert((db != NULL) && (*db != NULL));
   db->LastError = DB_SUCCESS;
   return LstIsEmpty(db->Tables);
}

I get a compilation error or (*db != NULL):
"DataManagement.c:38: no match for `DB & != int'"
So maybe (db != NULL) is enough?

P.S.  I also tried ((*db) != NULL) - produces same error.
0
 
sunnycoderCommented:
*db should be something that can be compared to an NULL ... I suspect *db is a struct ... Compiler cannot compare struct to an int and hence the error ...

To ensure that *db is valid, you will have to write some struct specific checks which are semantically in sync with your application and data structures.

db != NULL will make sure that you are not manipulating a NULL pointer
0
 
slaviknAuthor Commented:
ok. tnx.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now