Pointer Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert.C.Green@gmail.com

    #1

    Pointer Question

    I am currently passing multiple pointers to a function. Inside the
    function I allocate the memory for these pointers. Once the function
    completes and I return to the calling function, the pointers act as
    though they have not been created. Can anyone tell me why this is
    occuring? Thanks.

  • =?iso-8859-1?q?Erik_Wikstr=F6m?=

    #2
    Re: Pointer Question

    On 20 Mar, 14:06, Robert.C.Gr...@ gmail.com wrote:
    I am currently passing multiple pointers to a function. Inside the
    function I allocate the memory for these pointers. Once the function
    completes and I return to the calling function, the pointers act as
    though they have not been created. Can anyone tell me why this is
    occuring? Thanks.
    Let me guess, you have something like this:

    void foo(MyClass1* ptr1, MyClass2* ptr2)
    {
    ptr1 = new MyClass1();
    ptr2 = new MyClass2();
    }

    It's because you pass the pointers by value, so when you change them
    inside the function (with new) it's only local copies of the pointers
    you passed as parameters. You need to pass the pointers as references,
    I think it's like this:

    void foo(MyClass1*& ptr1, MyClass2*& ptr2);

    --
    Erik Wikström

    Comment

    • tragomaskhalos

      #3
      Re: Pointer Question

      On Mar 20, 1:06 pm, Robert.C.Gr...@ gmail.com wrote:
      I am currently passing multiple pointers to a function. Inside the
      function I allocate the memory for these pointers. Once the function
      completes and I return to the calling function, the pointers act as
      though they have not been created. Can anyone tell me why this is
      occuring? Thanks.
      I'm guessing you're getting confused between pointers and pointers-to-
      pointers and doing something like this:

      void alloc(Thing* p)
      {
      p = new Thing; // WRONG
      }
      void f()
      {
      Thing* p;
      alloc(p); // WRONG
      p->use();
      }

      Whereas you need to do this:
      void alloc(Thing** p)
      {
      *p = new Thing;
      }
      void f()
      {
      Thing* p;
      alloc(&p);
      p->use();
      }

      or this:
      void alloc(Thing*& p)
      {
      p = new Thing;
      }
      void f()
      {
      Thing* p;
      alloc(p);
      p->use();
      }


      Comment

      • Patrick

        #4
        Re: Pointer Question

        Does this code help

        void foo();

        void foo(int *Test,int * Test2)
        {
        Test = new int; // Create new int
        Test2 = new int;
        *Test2 =22;
        }

        int main(int argc, char* argv[])
        {
        int *Test;
        int *Test2;

        foo(Test,Test);

        *Test = *Test2;

        cout << Test;

        return 0;
        }

        Pat

        Comment

        • Fred Kleinschmidt

          #5
          Re: Pointer Question


          "Patrick" <hornepat2@gmai l.comwrote in message
          news:1174396733 .051827.80520@e 1g2000hsg.googl egroups.com...
          Does this code help
          >
          void foo();
          >
          void foo(int *Test,int * Test2)
          {
          Test = new int; // Create new int
          Test2 = new int;
          *Test2 =22;
          }
          >
          int main(int argc, char* argv[])
          {
          int *Test;
          int *Test2;
          >
          foo(Test,Test);
          >
          *Test = *Test2;
          No, it doesn't help. Test2 is uninitialized.
          >
          cout << Test;
          >
          return 0;
          }
          >
          --
          Fred L. Kleinschmidt
          Boeing Associate Technical Fellow
          Aero Stability and Controls Computing


          Comment

          • C C++ C++

            #6
            Re: Pointer Question

            On Mar 20, 9:06 am, Robert.C.Gr...@ gmail.com wrote:
            I am currently passing multiple pointers to a function. Inside the
            function I allocate the memory for these pointers. Once the function
            completes and I return to the calling function, the pointers act as
            though they have not been created. Can anyone tell me why this is
            occuring? Thanks.
            Are you passing the reference of variable which need to be allocated ?

            Regards,
            M.Azmath



            Comment

            • Lionel B

              #7
              Re: Pointer Question

              On Tue, 20 Mar 2007 06:18:53 -0700, Patrick wrote:
              Does this code help
              No, it's wrong (you obviously never tried even to compile, let alone run
              it)

              #include <iostream// for std::cout
              void foo();
              This is unnecessary
              void foo(int *Test,int * Test2)
              void foo(int*& Test,int*& Test2) // must pass by reference
              {
              Test = new int; // Create new int
              Test2 = new int;
              *Test2 =22;
              }
              >
              int main(int argc, char* argv[])
              {
              int *Test;
              int *Test2;
              >
              foo(Test,Test);
              foo(Test,Test2) ; // I think this is what you intended
              *Test = *Test2;
              >
              cout << Test;
              std::cout << *Test; // assuming you wanted to print the value
              return 0;
              }
              --
              Lionel B

              Comment

              Working...