Solved

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

Posted on 2004-08-18
8
356 Views
Last Modified: 2010-04-15
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
Comment
Question by:slavikn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11829096
Hi slavikn,

 Assert(lst != NULL);

Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11829143
>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
 
LVL 1

Author Comment

by:slavikn
ID: 11829173
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
Technology Partners: 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!

 
LVL 45

Accepted Solution

by:
sunnycoder earned 30 total points
ID: 11829180
>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
 
LVL 1

Author Comment

by:slavikn
ID: 11829221
Ok. I just wanted to be sure I was right. Thanks.
0
 
LVL 1

Author Comment

by:slavikn
ID: 11829373
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11829514
*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
 
LVL 1

Author Comment

by:slavikn
ID: 11829537
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question