ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
checking `text' for non-zero value to make sure that it is pointing to some location, checking `*text' for non-zero value to make sure that it has not reached the end of the string.
text is a pointer to a string - the value of text is either a memory address, or null. The first half of the if tests whether text contains an actual address. testing *text then tests the value at that location - this will succeed if there is any data there, or fail if the string is empty (the value at *text will be 0 to terminate the string)
So if( text && *text ) literally says "if text is a valid pointer AND text points to a non-empty string".
Everything said above is right, just one more interesting thing.
In the statement (text && *text) the second part '*text' will not be evaluated if the first part 'text' is NULL, sometimes called "short circuiting AND logic". This prevents a runtime core dump in the case when text is NULL, because evaluating *(NULL) is not allowed. Normally and logically, AND operations can be performed in any order and get the same result, but not in this case because the second '*text' can not be evaluated if the first part is false.
This is a very important point and a frequent source of software bugs.
Just to confirm that I understand. I can't eveluate *text without evaluating text because that't the rule. The pointer has to be tested before the value that pointer points to is tested. Do I get it right?
Also if I do like that:
if (*text)
then it still compiles, but I get segmentation fault during execution. What exactly happens?
answer to: what happens if you dereference text without testing to see if it's NULL?
The compiler will let you dereference the pointer, but if it turns out to be NULL (an invalid address for you to dereference), your program will end abruptly (segmentation fault).
Testing to see if that pointer is NULL or not is not enforced by the compiler, but is never a bad idea. It's a short operation for the computer to perform, and can be the difference between gracefully handling the error or crashing without output.
Even if you know the source of the pointer, the "is this pointer NULL" test (immediately before the pointer's use) is good practice. If the code is shuffled around in the future, or copied by someone else, the source of the pointer may change. For the 2 dead-simple CPU instructions that will compile into (compare and conditional branch), just test the pointer.
Just another note while you're reading about what you can and can't do with pointers:
NULL (zero) is not the only value of a pointer that you're not allowed to dereference. Any value of pointer that doesn't fall within a specific range of addresses will cause a segmentation fault when dereferenced (dereferencing, by the way, is the fancy say of saying "we check the contents of the address the pointer is equal to"). An invalid pointer can be the result of an uninitialized variable, incorrect pointer arithmetic, indexing an array improperly, or memory corruption elsewhere in the running process. The first three ways are preventable through good practice and correct code; the last one normally is too, but could potentially be caused by faulty code/libraries elsewhere.
This is an overkill response, I know, but there are going to be novice programmers reading this thread, and these are all good things to know when tracking/avoiding bugs
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.