Solved

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

Posted on 2004-08-18
8
351 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
  • 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now