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. Is it ok to use uninitialized pointer?

Is it ok to use uninitialized pointer?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionannouncement
10 Posts 7 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.
  • S Offline
    S Offline
    Sameerkumar Namdeo
    wrote on last edited by
    #1

    I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?

    int func_2(int i, float j)
    {
    cout << i << j << endl;
    return i;
    }

    class ABC
    {
    public:
    int func_1(int i, float j)
    {
    return func_2(i, j);
    }
    };

    [1] ABC *pObj;
    [2] int i = 10;
    [3] float f = 20.50f;
    [4] int r = pObj->func_1(i, f);

    When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:

    Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.

    but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.

    _ L S M 4 Replies Last reply
    0
    • S Sameerkumar Namdeo

      I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?

      int func_2(int i, float j)
      {
      cout << i << j << endl;
      return i;
      }

      class ABC
      {
      public:
      int func_1(int i, float j)
      {
      return func_2(i, j);
      }
      };

      [1] ABC *pObj;
      [2] int i = 10;
      [3] float f = 20.50f;
      [4] int r = pObj->func_1(i, f);

      When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:

      Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.

      but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Technically it can be used this way. But if such a situation arises, you should make the function a static function and call it using the class scope operator. The error is issued because these things usually tend to become bugs.

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      R 1 Reply Last reply
      0
      • _ _Superman_

        Technically it can be used this way. But if such a situation arises, you should make the function a static function and call it using the class scope operator. The error is issued because these things usually tend to become bugs.

        «_Superman_»
        I love work. It gives me something to do between weekends.

        Microsoft MVP (Visual C++)

        Polymorphism in C

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        I thought there must be an instance of an object to use a pointer to one. Even with a static member that is not the best syntax to use though. I hesitate to say correct because I am not a language expert. To use a static method the usual syntax is ClassName::MethodName(); when no instance is referenced.

        C 1 Reply Last reply
        0
        • R Rick York

          I thought there must be an instance of an object to use a pointer to one. Even with a static member that is not the best syntax to use though. I hesitate to say correct because I am not a language expert. To use a static method the usual syntax is ClassName::MethodName(); when no instance is referenced.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Rick York wrote:

          I thought there must be an instance of an object to use a pointer to one.

          If the function which is called do not access any members of the class, then it is "safe". But of cours it is absolutely bad practice.

          Rick York wrote:

          Even with a static member that is not the best syntax to use though.

          This, on the other hand is perfectly acceptable. I should take a coffee before answering questions on CP :) . This is not that acceptable, since the static function should accessed this way: ClassName::MethodName()

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          modified on Thursday, August 5, 2010 3:07 AM

          CPalliniC 1 Reply Last reply
          0
          • C Cedric Moonen

            Rick York wrote:

            I thought there must be an instance of an object to use a pointer to one.

            If the function which is called do not access any members of the class, then it is "safe". But of cours it is absolutely bad practice.

            Rick York wrote:

            Even with a static member that is not the best syntax to use though.

            This, on the other hand is perfectly acceptable. I should take a coffee before answering questions on CP :) . This is not that acceptable, since the static function should accessed this way: ClassName::MethodName()

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            modified on Thursday, August 5, 2010 3:07 AM

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Cedric Moonen wrote:

            This, on the other hand is perfectly acceptable.

            However, the wise developer would use the classname::method syntax. :)

            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]

            In testa che avete, signor di Ceprano?

            C 1 Reply Last reply
            0
            • CPalliniC CPallini

              Cedric Moonen wrote:

              This, on the other hand is perfectly acceptable.

              However, the wise developer would use the classname::method syntax. :)

              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]

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              I think I was not awake when I replied, I really thought he was talking about accessing the static function using the classname::method syntax. Pff, ok, I'll fetch a coffee now.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              CPalliniC 1 Reply Last reply
              0
              • C Cedric Moonen

                I think I was not awake when I replied, I really thought he was talking about accessing the static function using the classname::method syntax. Pff, ok, I'll fetch a coffee now.

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                :) I can barely reach my workstation, without caffein. :laugh:

                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]

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • S Sameerkumar Namdeo

                  I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?

                  int func_2(int i, float j)
                  {
                  cout << i << j << endl;
                  return i;
                  }

                  class ABC
                  {
                  public:
                  int func_1(int i, float j)
                  {
                  return func_2(i, j);
                  }
                  };

                  [1] ABC *pObj;
                  [2] int i = 10;
                  [3] float f = 20.50f;
                  [4] int r = pObj->func_1(i, f);

                  When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:

                  Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.

                  but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  With the risk of being wordy... This piece of code should illustrate things.

                  class Z
                  {
                  public:
                  int g;
                  void out(){printf("addr %p\n", this);}
                  void outg(){printf("addr %p g=%d\n", this, g);}
                  };

                  Z *pz1, *pz2=0, *pz3=(Z*)0xdeadbabe, *pz4, z;

                  void main()
                  {
                  pz1->out();// line 1
                  pz2->out();// line 2
                  pz3->out();// line 3
                  pz4->out();// line 4
                  z.g=100;// line 5
                  pz1=&z;// line 6
                  pz1->outg();// line 7
                  pz2->outg();// line 8
                  pz3->outg();// line 9
                  pz4->outg();// line 10
                  }

                  As you can see, pz1 to pz4 are either not initialized or are initialized to "wrong" values. However, lines 1 to 4 execute happily, even though you dereferance using these wrong pointers. Thats because you aren't actually touching the member variable at all. Now, look at what happens next. At line 6, pz1 is initiated to a valid address and hence outg() doesn't complain. Why should it complain you may ask. I'll tell you. Open the disassembly window and step into the function call at line 8. There you'll find that the control actually steps into the function, even when derefeneced using a wrong pointer! Thats because one line of C/C++ translates into many many lines of assembler. As you step past each assembler instruction, you'll encounter this instruction

                  mov ecx, dword ptr [eax]

                  and that is where you get the tantrum. In this line, the member variable 'g' is attempted to be accessed. EAX really holds the address of the object but because 'g' is the first member, the address coincide. And the complaint is valid as you cannot CANNOT SHOULD NOT access data from uninitialized memory. EDIT: I forgot to mention that you have to turn off "incremental linking". It doesn't matter much except that if its on you might get confused and/or distracted by the " @ILT+nnn " that you'll encounter. An oh, its MSVC6.0

                  ...byte till it megahertz...

                  modified on Thursday, August 5, 2010 3:33 AM

                  1 Reply Last reply
                  0
                  • S Sameerkumar Namdeo

                    I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?

                    int func_2(int i, float j)
                    {
                    cout << i << j << endl;
                    return i;
                    }

                    class ABC
                    {
                    public:
                    int func_1(int i, float j)
                    {
                    return func_2(i, j);
                    }
                    };

                    [1] ABC *pObj;
                    [2] int i = 10;
                    [3] float f = 20.50f;
                    [4] int r = pObj->func_1(i, f);

                    When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:

                    Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.

                    but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.

                    S Offline
                    S Offline
                    Sameerkumar Namdeo
                    wrote on last edited by
                    #9

                    Thanks to all of you.

                    My Blog.
                    My codeproject Articles.

                    1 Reply Last reply
                    0
                    • S Sameerkumar Namdeo

                      I know its not correct. -> But can a pointer, to a class which contains only member functions and absolutely no data members, be used to invoke its member functions?

                      int func_2(int i, float j)
                      {
                      cout << i << j << endl;
                      return i;
                      }

                      class ABC
                      {
                      public:
                      int func_1(int i, float j)
                      {
                      return func_2(i, j);
                      }
                      };

                      [1] ABC *pObj;
                      [2] int i = 10;
                      [3] float f = 20.50f;
                      [4] int r = pObj->func_1(i, f);

                      When the above code is executed using VC++2003(version 7.1) following error occurs at line[4]:

                      Run-Time Check Failure #3 - The variable 'pObj' is being used without being defined.

                      but even if the error is ignored\ continued then also every thing work properly. Please suggest : ->Whether this method of using uninitialized pointer is acceptable in any condition? ->Will this work on different platforms?(could not try this, since I do not have accesses to other platform) ->Any pitfalls? ->Your Valuable suggestions. Thanks in advance.

                      M Offline
                      M Offline
                      MuraliKrishnaP
                      wrote on last edited by
                      #10

                      ccpptrain wrote:

                      ->Whether this method of using uninitialized pointer is acceptable in any condition?

                      It is acceptable as long as you are careful, as you promised absolutely no data members. With this condition, you initialize or don't initialize or simply call using 0 (zero) pointer, it is acceptable.

                      ccpptrain wrote:

                      ->Will this work on different platforms?

                      It is same for all platforms.

                      ccpptrain wrote:

                      ->Any pitfalls?

                      One more promise: don't delete the pObj.

                      ccpptrain wrote:

                      ->Your Valuable suggestions.

                      Don't do like this. You never know when you are going to add member variables and access them.

                      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