Pointer to Pointer or not?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nifsmith

    #1

    Pointer to Pointer or not?

    Hi

    I am creating my own Queue class to learn about Queues and pointers.

    I have come across a question of two styles and I don't know if there
    are any dangers associated with them.

    I coded my remove function as follows

    template <class T>
    bool adsQ<T>::Remove (T* theData)
    {
    Qnode<T>* removeNode = 0;

    if (true == isEmpty())
    {
    //cout << "False" << endl;
    return false;
    }
    else
    {
    /* What I am trying to achive here is to change the data pointed to
    by theData to the data pointed to by pData.*/

    *theData =*(pHead->pData); //Data exchanged

    removeNode = pHead;

    if (count == 1)
    {
    pHead = 0; //Pointers to Null
    pTail = 0;
    }
    else
    {
    pHead = pHead->nextQnode;
    }

    count--;

    /*I then free up the memory which contained the pHead, pData.
    Knowing that *theData now contains the same data as pData did*/

    delete removeNode;

    removeNode = 0; //Make pointer safe

    //cout << "True" << endl;
    return true;
    }
    }

    I then read about pointers to pointers and amended my function signature

    template <class T>
    bool adsQ<T>::Remove (T** theData) ...

    and the line where the data is exchanged.

    *theData = pHead->pData;

    And called the function thus

    pQ->Remove(&pD);

    I now appear to get the same results from both, the returning of the
    data contained in the pointer pData outwith the function.

    However in changing the function to take a pointer to pointer am I
    storing up trouble for myself in that as the free space pointed to by
    pData is now pointed at by pD outwith the function (is this right?) and
    also pData within the function, can I now delete it within the function
    (through removeNode) safely or is this trouble as I appear to be
    deleting memory pointed at by two pointers.

    TYIA

    nifsmith
  • Jari Kalevi Savolainen

    #2
    Re: Pointer to Pointer or not?

    On Sun, 3 Oct 2004, nifsmith wrote:
    [color=blue]
    > Hi
    >
    > I am creating my own Queue class to learn about Queues and pointers.
    >
    > I have come across a question of two styles and I don't know if there
    > are any dangers associated with them.
    >
    > I coded my remove function as follows
    >
    > template <class T>
    > bool adsQ<T>::Remove (T* theData)
    > {
    > Qnode<T>* removeNode = 0;
    >
    > if (true == isEmpty())
    > {
    > //cout << "False" << endl;
    > return false;
    > }
    > else
    > {
    > /* What I am trying to achive here is to change the data pointed to
    > by theData to the data pointed to by pData.*/
    >
    > *theData =*(pHead->pData); //Data exchanged
    >
    > removeNode = pHead;
    >
    > if (count == 1)
    > {
    > pHead = 0; //Pointers to Null
    > pTail = 0;
    > }
    > else
    > {
    > pHead = pHead->nextQnode;
    > }
    >
    > count--;
    >
    > /*I then free up the memory which contained the pHead, pData.
    > Knowing that *theData now contains the same data as pData did*/
    >
    > delete removeNode;
    >
    > removeNode = 0; //Make pointer safe
    >
    > //cout << "True" << endl;
    > return true;
    > }
    > }
    >
    > I then read about pointers to pointers and amended my function signature
    >
    > template <class T>
    > bool adsQ<T>::Remove (T** theData) ...
    >
    > and the line where the data is exchanged.
    >
    > *theData = pHead->pData;
    >
    > And called the function thus
    >
    > pQ->Remove(&pD);
    >
    > I now appear to get the same results from both, the returning of the
    > data contained in the pointer pData outwith the function.
    >
    > However in changing the function to take a pointer to pointer am I
    > storing up trouble for myself in that as the free space pointed to by
    > pData is now pointed at by pD outwith the function (is this right?) and
    > also pData within the function, can I now delete it within the function
    > (through removeNode) safely or is this trouble as I appear to be
    > deleting memory pointed at by two pointers.
    >
    > TYIA
    >
    > nifsmith
    >[/color]

    In the first aproach you are dereferreing the pointer and assignin to
    instance it points. It'll call assign operator of type it points to. So
    mem will be copied and you can delete original 'safely'.

    In second version done by pointers to pointers only the address of
    instance is assigned. Deleting original now would result in a pointer to
    free mem.

    I hope I'm making sense to you.

    Comment

    • John Harrison

      #3
      Re: Pointer to Pointer or not?


      "nifsmith" <hawklord451@ly cos.co.uk> wrote in message
      news:cjodqs$npq $1@titan.btinte rnet.com...[color=blue]
      > Hi
      >
      > I am creating my own Queue class to learn about Queues and pointers.
      >
      > I have come across a question of two styles and I don't know if there are
      > any dangers associated with them.
      >
      > I coded my remove function as follows
      >
      > template <class T>
      > bool adsQ<T>::Remove (T* theData)
      > {
      > Qnode<T>* removeNode = 0;
      >
      > if (true == isEmpty())
      > {
      > //cout << "False" << endl;
      > return false;
      > }
      > else
      > {
      > /* What I am trying to achive here is to change the data pointed to by
      > theData to the data pointed to by pData.*/
      >
      > *theData =*(pHead->pData); //Data exchanged
      >
      > removeNode = pHead;
      >
      > if (count == 1)
      > {
      > pHead = 0; //Pointers to Null
      > pTail = 0;
      > }
      > else
      > {
      > pHead = pHead->nextQnode;
      > }
      >
      > count--;
      >
      > /*I then free up the memory which contained the pHead, pData. Knowing that
      > *theData now contains the same data as pData did*/
      >
      > delete removeNode;
      >
      > removeNode = 0; //Make pointer safe
      >
      > //cout << "True" << endl;
      > return true;
      > }
      > }
      >
      > I then read about pointers to pointers and amended my function signature
      >
      > template <class T>
      > bool adsQ<T>::Remove (T** theData) ...
      >
      > and the line where the data is exchanged.
      >
      > *theData = pHead->pData;
      >
      > And called the function thus
      >
      > pQ->Remove(&pD);
      >
      > I now appear to get the same results from both, the returning of the data
      > contained in the pointer pData outwith the function.
      >
      > However in changing the function to take a pointer to pointer am I storing
      > up trouble for myself in that as the free space pointed to by pData is now
      > pointed at by pD outwith the function (is this right?) and also pData
      > within the function, can I now delete it within the function (through
      > removeNode) safely or is this trouble as I appear to be deleting memory
      > pointed at by two pointers.
      >[/color]

      You are getting into trouble with your pointer to pointer method. You are
      deleting memory in your Remove function which is still accessible from
      outside your Remove function. This is likely to crash your program.

      This is a design issue, and the issue is, who owns the pointers. If you
      consider that the queue owns the pointers then it should delete them and you
      should go back to your original method. Alternatively you could say that the
      program using the queue owns the pointers, in this case you can use your
      pointer to pointer method, but then the queue should not delete the memory,
      that would now be the calling programs responsibility.

      You don't say how the memory is allocated originally, is that done in the
      queue class, or outside the queue class, but I would guess that the
      allocation is being done in the queue (anything else would be poor design).
      In that case it seems natural for the queue to own the pointers and do the
      deallocation itself. That way the user of the queue doesn't have to worry
      about memory allocation at all, which make the queue easier to use.

      So, in short, I would say that you should go back to your original idea.

      john


      Comment

      Working...