dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Checking For Valid Pointers

  1. #1
    Join Date
    Mar 2012
    Posts
    4

    Question 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:


    void CalcArea(const double* const ptrPi, // const pointer to const data
    const double* const ptrRadius, // i.e. no changes allowed
    double* const ptrArea) // changes to data pointed to allowed
    {
    // check the pointers for validity before using
    if (ptrPi && ptrRadius && ptrArea)
    *ptrArea = (*ptrPi) * (*ptrRadius) * (*ptrRadius);
    }



    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?

    Andre

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,416

    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!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

  3. #3
    Join Date
    Mar 2012
    Posts
    4

    Question 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)

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,750

    Re: Checking For Valid Pointers

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Feb 2017
    Posts
    344

    Re: Checking For Valid Pointers

    Quote Originally Posted by astevens009 View Post
    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?
    In C++ there's something called implicit conversions,

    https://en.cppreference.com/w/cpp/la...cit_conversion

    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,
    Code:
    if ((ptrPi != nullptr) && (ptrRadius != nullptr) && (ptrArea != nullptr))
    The "pointer to bool" conversion makes this shorter and that's why it's used.

    Note that the nullptr keyword was introduced with the C++ 11 standard in 2011. Textbooks written before that obviously don't mention it.
    Last edited by wolle; June 14th, 2018 at 01:31 AM.

  6. #6
    Join Date
    Feb 2017
    Posts
    344

    Re: Checking For Valid Pointers

    Quote Originally Posted by astevens009 View Post
    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.
    Last edited by wolle; June 14th, 2018 at 01:33 AM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,416

    Re: Checking For Valid Pointers

    Quote Originally Posted by astevens009 View Post
    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!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)