Solved

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

Posted on 2004-08-18
8
349 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to create project in Eclipse ? 1 184
negation in C function 14 132
Coverting 24 hour time to 12 hour in C++ 15 162
Problem to ASCII 1 148
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

760 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