Thread: Refresh me.. Double pointer's question

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Refresh me.. Double pointer's question

    I haven't coded in a while, months. Anyway I'm back and I need a little refresher.

    If I have 2 pointers:
    Type* ptr1;
    Type* ptr2;

    and set ptr2 using new, and set ptr1 to point to ptr2, how do I delete ptr2 through ptr1? I was thinking delete *ptr1 or delete &ptr1; The latter works syntactically, but not sure if it's doing what I want. Is that correct? If not, what is? Thanks

    Code:
    Type* ptr1;
    Type* ptr2;
    ptr2 = new Type;
    ptr1 = ptr2;
    Using Dev-C++ on Windows

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just as you would delete ptr2:
    Code:
    delete ptr1;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    But wouldn't just just delete ptr1 because it's also a pointer? Or does the compiler know to delete ptr2 because ptr1 wasn't allocated with new?
    Using Dev-C++ on Windows

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    delete will allow the memory that ptr1 and ptr2 point to return to the memory pool. Just remember that every new needs a delete somewhere. Remember to set the pointers to NULL.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    new returns a memory address to newly allocated memory. Setting ptr1 to ptr2 means that ptr1 also points to this memory address therefore ptr1 and ptr2 not only share the same content but also the same memory address; therefore, "deleting" ptr1 will also "delete" ptr2.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... I'm not sure I agree.
    Code:
    int* ptr = new int(12);
    int* ptr2 = ptr;
    
    delete ptr2; ptr2 = 0;
    
    *ptr = 24; // bad
    
    std::cout << *ptr << "\t" << ptr << std::endl; // Danger: This still goes through!
    std::cout << *ptr2 << "\t" << ptr2 << std::endl; // Only here an Access Violation is detected
    EDIT: Hmm... I didn't set ptr to 0. Forget it. I have a dangling ptr. It was still deleted.
    Last edited by Mario F.; 07-15-2006 at 06:40 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Which brings up the purpose of auto_ptr...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with double pointers
    By cat1 in forum C Programming
    Replies: 4
    Last Post: 05-06-2009, 01:28 AM
  2. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM