Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Doubt in using NULL pointer

Doubt in using NULL pointer

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 5 Posters 0 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.
  • K Offline
    K Offline
    KASR1
    wrote on last edited by
    #1

    class CAdd { public: CAdd(){}; ~CAdd(){}; void Display( ){ } }; CAdd pObj = NULL; pObj->Display(); Here pObj is set to NULL but call to Display() succeed how?

    T C 2 Replies Last reply
    0
    • K KASR1

      class CAdd { public: CAdd(){}; ~CAdd(){}; void Display( ){ } }; CAdd pObj = NULL; pObj->Display(); Here pObj is set to NULL but call to Display() succeed how?

      T Offline
      T Offline
      Tim Craig
      wrote on last edited by
      #2

      Debug or Release version? If Release, the compiler may have been smart enough to see the function didn't do anything and optimized the whole call away.

      You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

      K 1 Reply Last reply
      0
      • K KASR1

        class CAdd { public: CAdd(){}; ~CAdd(){}; void Display( ){ } }; CAdd pObj = NULL; pObj->Display(); Here pObj is set to NULL but call to Display() succeed how?

        C Offline
        C Offline
        Cool_Dev
        wrote on last edited by
        #3

        CAdd *pObj = NULL; pObj->Display(); Call will be succeeded as long as none of the member variables of CAdd is referred in Display(). When pgm loads, a class will be having only one copy of all its member functions for all class variables we create, but will be having its own copy of member variables for each class variables. So members variables will be allocated only when an instance of class is created but there will be already a copy of all member functions even if no instance of class is created. Hope you understood how the calls to Display is succeeded above.

        K 1 Reply Last reply
        0
        • T Tim Craig

          Debug or Release version? If Release, the compiler may have been smart enough to see the function didn't do anything and optimized the whole call away.

          You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

          K Offline
          K Offline
          KASR1
          wrote on last edited by
          #4

          In executes debug as well as in release mode. Even if write some statements in Display() function it will execute.Its not because of empty function.

          1 Reply Last reply
          0
          • C Cool_Dev

            CAdd *pObj = NULL; pObj->Display(); Call will be succeeded as long as none of the member variables of CAdd is referred in Display(). When pgm loads, a class will be having only one copy of all its member functions for all class variables we create, but will be having its own copy of member variables for each class variables. So members variables will be allocated only when an instance of class is created but there will be already a copy of all member functions even if no instance of class is created. Hope you understood how the calls to Display is succeeded above.

            K Offline
            K Offline
            KASR1
            wrote on last edited by
            #5

            Yes I understood your explanation. But still i am not clear with Display() function call with NULL object.

            C K C 3 Replies Last reply
            0
            • K KASR1

              Yes I understood your explanation. But still i am not clear with Display() function call with NULL object.

              C Offline
              C Offline
              Cool_Dev
              wrote on last edited by
              #6

              compiler just examines the type of pointer and so redirect the function call to the location where Display() is loaded. You just write some statements in Display(), put a brake point there and examine 'this' pointer. You can see it as NULL. Whenever you access any of the member variables, then 'this' pointer will be used, and as it is NULL an access violation will be occurred.

              T 1 Reply Last reply
              0
              • K KASR1

                Yes I understood your explanation. But still i am not clear with Display() function call with NULL object.

                K Offline
                K Offline
                KingsGambit
                wrote on last edited by
                #7

                CAdd* pObj = NULL; pObj->Display(); Compiler actuall transforms the call to some thing as follows: void Display(CAdd * const this) { ... } so, I think as long as Display() is not accessing any of the members of the class CAdd, it is safe to call Display() with a NULL ptr.

                1 Reply Last reply
                0
                • K KASR1

                  Yes I understood your explanation. But still i am not clear with Display() function call with NULL object.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Because even if you are a death man, you can still breath, because your're still a member of the species. Well, you know, the OOP mapping to the real world should break at some point... :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • C Cool_Dev

                    compiler just examines the type of pointer and so redirect the function call to the location where Display() is loaded. You just write some statements in Display(), put a brake point there and examine 'this' pointer. You can see it as NULL. Whenever you access any of the member variables, then 'this' pointer will be used, and as it is NULL an access violation will be occurred.

                    T Offline
                    T Offline
                    Tim Craig
                    wrote on last edited by
                    #9

                    Making Display virtual would make a much more interesting problem. :cool: The pointer to the object is NULL so the v-table pointer would be invalid and should crash and certainly would if the nonexistent class instance was being called through a base class pointer somewhere. However, in this case the compiler could verify the actual type of the object and go directly to the v-table and make the call in the name of efficiency.

                    You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups