Solved

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

Posted on 2004-08-18
8
353 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

827 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