CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2012
    Posts
    2

    Passing NULL to a pointer parameter

    Here is an example:

    Public:
    double firstCall(void *a, int *b)
    {
    return internalCall(a, b, NULL, NULL)
    }

    double secondCall(void *a, int *b, void *c, int *d)
    {
    return internalCall(a, b, c, d)
    }

    Private:
    double internalCall(void *a, int *b, void *c, int *d)
    {
    .....
    if (NULL != c)
    work with c and d
    }

    I want to call firstCall(...) and secondCall(...) randomaly about 500 times.

    I tested this code and seems it works. Is it a good idea to pass NULL as shown above? If not, what would be a good way to get expected outcome? I am just trying to reduce duplicate codes and kind of afraid if that gives a memory or program problem.

    I am new to c++ and wanted to make sure this does not create a problem. I would appreciate if someone help me out.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Passing NULL to a pointer parameter

    Quote Originally Posted by DevFellow View Post
    Code:
    Public:
              double firstCall(void *a, int *b)
              {
                   return internalCall(a, b, NULL, NULL)
               }
              ...
    
    Private:
             double internalCall(void *a, int *b, void *c, int *d)
             {
                  .....
                  if (NULL != c)
                         work with c and d
              }
    ... Is it a good idea to pass NULL as shown above?
    Why not?
    You also could declare your internalCall as
    Code:
    double internalCall(void *a, int *b, void *c = NULL, int *d = NULL);
    PS: please, use Code tags while posting code snippets!
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2012
    Posts
    2

    Re: Passing NULL to a pointer parameter

    Thanks VictorN

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Passing NULL to a pointer parameter

    Your parameters can have any value including NULL. What you have to do is make sure you don't use NULL pointers to access objects. That would crash your application.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Aug 2006
    Posts
    231

    Re: Passing NULL to a pointer parameter

    It's recommended to pass nullptr instead of NULL macro.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Passing NULL to a pointer parameter

    Quote Originally Posted by TubularX View Post
    It's recommended to pass nullptr instead of NULL macro.
    nullptr in Visual C++? In native code?
    Note that nullptr can be used in VS2005 and VS2008 only when compiled with /clr option which in not used for the most of the project types discussed of this Forum.

    And nullptr should not be used for native code in VS2010. There is a __nullptr instead (Microsoft-specific keyword)
    Victor Nijegorodov

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Passing NULL to a pointer parameter

    Quote Originally Posted by VictorN
    And nullptr should not be used for native code in VS2010. There is a __nullptr instead (Microsoft-specific keyword)
    Hmm... that is somewhat troublesome for native code that is intended to be portable though since nullptr is now a standard C++ 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

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Passing NULL to a pointer parameter

    Quote Originally Posted by laserlight View Post
    Hmm... that is somewhat troublesome for native code that is intended to be portable though since nullptr is now a standard C++ keyword.
    Well, I'm not a guru in the modern C++ features, but it is what and how I've understood it from MSDN:
    The __nullptr keyword is a Microsoft-specific keyword that has the same meaning as nullptr, but applies to only native code. If you use nullptr with native C/C++ code and then compile with the /clr compiler option, the compiler cannot determine whether nullptr indicates a native or managed null pointer value. To make your intention clear to the compiler, use nullptr to specify a managed value or __nullptr to specify a native value.
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Passing NULL to a pointer parameter

    Quote Originally Posted by VictorN
    Well, I'm not a guru in the modern C++ features, but it is what and how I've understood it from MSDN:
    Yeah, I read that MSDN entry before coming to my conclusion. That said, reading it again, I think that the way out is to simply use nullptr, but take care not to compile with /clr, which makes sense in my scenario since the code is intended to also be compiled by say, g++. Of course, in that case, your objection to TubularX's suggestion no longer holds.
    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

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Passing NULL to a pointer parameter

    nullptr is only available from VS2010.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured