CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2008
    Posts
    6

    How to delete a pointer

    Having a little trouble deleting a pointer, returns only jumbled up numbers...

    Code:
    void deletefn(nametype *head)
    {
        int option = 0;
        nametype *currptr, *deleteptr;
        currptr=head;
      
        while(option!=1 && currptr != NULL)
    {
    cout<<"\nCurrent product is: "<<currptr->description<<endl;
    cout<<"Press 1 to delete this product\n "
        <<"                0 to continue: ";
    cin>>option;
    
    if(option != 1) currptr=currptr->link;}
    
    if(option == 1)
    {  
        currptr = deleteptr;
        delete deleteptr;
        deleteptr = NULL;
    }
    
    else{cout<<"Could not add node to the link."<<endl;}
        
    
    }//End of deletefn

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to delete a pointer

    Quote Originally Posted by motorizedmedia
    Having a little trouble deleting a pointer, returns only jumbled up numbers...
    I only see a single function that returns void.

    http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: How to delete a pointer

    I don't know, what you are trying to achieve (your code is horribly indented and a function called deletefn that prints out a message "Could not add node to the link." does not make much sense in my mind), but deleteptr remains uninitialized throughout your function, so running delete on it might not have the desired effect.

    Most compilers issue warnings in case variables are used uninitialized. Either you did not read them or you should check your compiler settings.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: How to delete a pointer

    deleteptr.

    You are effcetively doing something like the following..

    Code:
    Member *deleteptr;
     
    anotherptr = deleteptr; // here deleteptr is uninitialized  dangling pointer
     
    delete deleteptr;// deleting a uninitialized pointer invoking undefined behaviour
    anotherptr->SomeMethod();//calling a method on a deleted pointer which was an uninitialized pointer// Ofcourse undefined behaviour...
    Dont forget to rate my post if you find it useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured