If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Checking For Valid Pointers

Hello.
I took a course in C++ some years ago but it's been some time since I used the language to code. I decided to take some time to review the language, and invested in the SAMS Teach Yourself C++ on One Hour a Day book. The following lines of code are used in a demo in Lesson 8 and I'm not sure how the if(...) statement works:

I know if(...) statements usually check for conditions like equality (i.e. - "if(x ==0){...}"), but in this case I'm not sure what it's checking for with ptrPi, ptrRadius and ptrArea. Do these variables return true or false as always? Or is there some other value that indirectly translates to true or false?

Re: Checking For Valid Pointers

ptrPi, ptrRadius and ptrArea are pointers. A pointer may be nullptr or it may have a valid memory address. The if statement is checking that all the pointers have a value and that none of them are nullptr. If a pointer (eg ptrPi) is not nullptr then the condition ptrPi is true. If ptrPi was nullptr then the condition ptrPi would be false. In c++, conditions are only evaluated until the result is known. So for an && (logical and) as soon as one evaluates to false, the whole condition becomes false and teh rest aren't evaluated.

What is the version of Sams Teach Yourself c++ are you using?

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: Checking For Valid Pointers

Originally Posted by astevens009

Thank you for your response. One more question, what if the pointer wasn't initialized and is pointing to some random address, would it still be considered "true"?

It might or might not be, so you shouldn't assume either way. Rather, you should declare variables near first use so that they can be initialised to point to something, and if that's not feasible, initialise them to be null pointers. Likewise, you should keep your pointers in the smallest scope necessary so that it is likely that at the time they no longer point to something valid, they would have gone out of scope anyway, and if that's not feasible, set them to be null pointers as soon as they are no longer valid.

Of course, this assumes that you actually need to deal with pointers like this. In the example given, you don't even need to deal with pointers at all since you could just have the double values passed by value, so presumably the example was just contrived to explain the differing placements of the const keyword.

Re: Checking For Valid Pointers

Originally Posted by astevens009

I know if(...) statements usually check for conditions like equality (i.e. - "if(x ==0){...}"), but in this case I'm not sure what it's checking for with ptrPi, ptrRadius and ptrArea. Do these variables return true or false as always? Or is there some other value that indirectly translates to true or false?

It's quite complicated and I use only the most common cases like for example "implicit conversion of pointer to bool" which is the one you're addressing. If the pointer holds nullptr the conversion to bool yields false, otherwise true. If not utilizing this conversion your example would be written like this,

Re: Checking For Valid Pointers

Originally Posted by astevens009

If the pointer wasn't initialized and is pointing to some random address, would it still be considered "true"?

No, the "pointer to bool" conversion bases the conversion solely on whether the pointer equals nullptr or not. If the pointer doesn't equal nullptr the conversion will yield true and if the pointer is then used but is invalid you have a bug on your hands.

I'm not sure it's even possible to check the validity of a pointer at runtime in C++. A garbage collector must be able to do that but there isn't one in standard C++. So it's up to the programmer to make sure each and every pointer is valid as long as it's used.

Re: Checking For Valid Pointers

Thank you for your response. One more question, what if the pointer wasn't initialized and is pointing to some random address, would it still be considered "true"?

NOTE: I am using the 8th edition of the SAMS Teach Yourself C++ book (author: Siddartha Rao)

The 8th edition is the latest and covers c++14 with a preview of c++17. It's not a book I am familiar with though.

When you define a variable, you should always initialize it as part of the definition. For a pointer, this should be either to a valid memory address (from new etc) or to nullptr.

If a pointer wasn't initialized and is pointing to some random address, then for the purpose of that if statement, yes it would be considered true (as it's not nullptr) and de-referencing the pointer(s) for the calculation would likely cause the program to generate a run-time exception. There is no c++ method of determining whether a given value is a 'valid' memory address or not.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.