Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Moved] A question about pointers.

    C++ Gurus
    6
    14
    8085
    Loading More Posts
    • 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.
    • C
      cazador7907 last edited by

      What is the proper way to initialize a pointer? Can it be initialized to NULL?

      Laurence -

      1 Reply Last reply Reply Quote 0
      • T
        Taamalus last edited by

        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 Reply Quote 0
        • C
          cazador7907 last edited by

          The the way to test for NULL is simply:

          If (pointer == null)

          or

          if (pointer == 0)

          Yes?

          Laurence -

          1 Reply Last reply Reply Quote 0
          • T
            Taamalus last edited by

            ^ 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 Reply Quote 0
            • T
              tobias.hunger last edited by

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

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

                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 Reply Quote 0
                • G
                  giesbert last edited by

                  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 Reply Quote 0
                  • A
                    andre last edited by

                    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 Reply Quote 0
                    • G
                      goetz last edited by

                      [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 Reply Quote 0
                      • T
                        tobias.hunger last edited by

                        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 Reply Quote 0
                        • G
                          giesbert last edited by

                          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 Reply Quote 0
                          • G
                            goetz last edited by

                            [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 Reply Quote 0
                            • G
                              giesbert last edited by

                              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 Reply Quote 0
                              • T
                                Taamalus last edited by

                                [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 Reply Quote 0
                                • First post
                                  Last post