Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Moved] A question about pointers.
Forum Updated to NodeBB v4.3 + New Features

[Moved] A question about pointers.

Scheduled Pinned Locked Moved C++ Gurus
14 Posts 6 Posters 9.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Taamalus
    wrote on last edited by
    #2

    Int[or whatever] * ptrName = NULL (or 0);

    That's what I do - It does not mean it's an absolute way. Usually I know what I want the pointer to do.

    ... time waits for no one. - Henry

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cazador7907
      wrote on last edited by
      #3

      The the way to test for NULL is simply:

      If (pointer == null)

      or

      if (pointer == 0)

      Yes?

      Laurence -

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Taamalus
        wrote on last edited by
        #4

        ^ This may not work.
        http://www.codeguru.com/forum/archive/index.php/t-358371.html

        Simply go
        @
        if (ptrName)
        cout << “Pointer points to something”;
        else
        cout << “Pointer is NULL”;
        @

        Yet, make sure, the pointer is initialized, as described in more detail in the link. ;)

        ... time waits for no one. - Henry

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #5

          You can of course also initialize member pointers in the initializer list of the constructor.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #6

            Initialization in the initializer list is a good idea. Did you know you can even new objects there? Things like this are legal:

            @
            MyQObject::MyQObject(parent):
            m_timer(new QTimer(this))
            {
            ...
            }
            @

            For pointers that you wish to initialize elsewhere: NULL is more of a C construct. For C++, using 0 is more common.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #7

              If you don't ant to check your pointers for 0, you can also use the so called NullObject pattern.

              This means you initialise a pointer to a static object with the same interface that just does nothing. The you can avoid this (if(0 != p) ...

              "see here":http://en.wikipedia.org/wiki/Null_Object_pattern

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #8

                Nice one Gerolf, I was not familiar with this pattern.
                Though of course you don't need to actually do if(0 != ptr) { }, as Taamalus points out.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #9

                  [quote author="Taamalus" date="1300504867"]^ This may not work.
                  http://www.codeguru.com/forum/archive/index.php/t-358371.html
                  [/quote]

                  What's the rationale behind this? To my knowledge even in poor old C (without ++) "0" is the all-valid constant for a null pointer (regardless of the internal representation for the actual machine). So

                  @
                  if(ptr == 0)
                  doFancyThings();
                  @

                  is always valid code (See "comp.lang.c FAQ list, Question 5.2":http://c-faq.com/null/null2.html and "Question 5.5":http://c-faq.com/null/machnon0.html).

                  Though the short comparison you suggested, is always valid too ("C FAQ, 5.3":http://c-faq.com/null/ptrtest.html) and also more readable, so to prefer over the "noisy" one, IMHO.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tobias.hunger
                    wrote on last edited by
                    #10

                    Somewhat unrelated, but the assumption that accessing the 0 pointer will always results in a segmentation fault is not necessarily true. See https://lwn.net/Articles/342330/ for a very interesting description of what can happen when 0 suddenly becomes a valid pointer.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #11

                      I also saw something like this:

                      @
                      int CClass::foo()
                      {
                      if(0 == this)
                      return 0
                      do stuff
                      }
                      @

                      with this syntax, this is absolutly safe:

                      @
                      CClass* p = 0;
                      p->foo();
                      @

                      This was some method in I think MFC API

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #12

                        [quote author="Gerolf" date="1300560358"]I also saw something like this:

                        @
                        int CClass::foo()
                        {
                        if(0 == this)
                        return 0
                        do stuff
                        }
                        @

                        with this syntax, this is absolutly safe:

                        @
                        CClass* p = 0;
                        p->foo();
                        @

                        This was some method in I think MFC API[/quote]

                        Completely weird... I would not suppose this to work on other compilers than Microsoft's :-)

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          giesbert
                          wrote on last edited by
                          #13

                          don't know, I think ist CWnd::getSafeHWnd...
                          and as no members are accessed, it could work.
                          This is in fact a parameter (hidden behind C++).

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            Taamalus
                            wrote on last edited by
                            #14

                            [quote author="Volker" date="1300533129"][quote author="Taamalus" date="1300504867"]^ This may not work.
                            http://www.codeguru.com/forum/archive/index.php/t-358371.html
                            [/quote]

                            What's the rationale behind this? To my knowledge even in poor old C (without ++) "0" is the all-valid constant for a null pointer (regardless of the internal representation for the actual machine). So

                            @
                            if(ptr == 0)
                            doFancyThings();
                            @

                            is always valid code (See "comp.lang.c FAQ list, Question 5.2":http://c-faq.com/null/null2.html and "Question 5.5":http://c-faq.com/null/machnon0.html).
                            [/quote]
                            Thanks, I stand corrected. :) Still, there is no way I will use it, but from now on, just as a personal preference.
                            http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer
                            this link is just to save face :D on my preferences

                            Also thanks Gerolf! Re: Null Object Patterns, not for C++ but for LISP! Cheers! :)

                            ... time waits for no one. - Henry

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved