Thread: Pointers

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    6

    Pointers

    What is hapening here:
    I can declare a pointer to cls1 and I can access (set) the cls1 object without 'new' operator?! Why?


    MyClass cls1;
    MyClass* pCls1 = &cls1;

    pCls1->set(int i);
    pCls1->printAll();

    delete pCls1;
    Last edited by djddj; 11-09-2006 at 12:30 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why?
    Why not? pCls1 points to a valid object. I hope someone didn't tell you that pointers have to be used with new and delete. That person is woefully misinformed.

    >delete pCls1;
    This, on the other hand, is very bad. pCls1 points to cls1, so using delete will try to free the memory allocated to cls1. However, since cls1 wasn't allocated using new, the behavior is undefined. With any luck it'll crash immediately and leave a nasty message in your face so that you can fix it.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Quote Originally Posted by Prelude
    >Why?
    Why not? pCls1 points to a valid object. I hope someone didn't tell you that pointers have to be used with new and delete. That person is woefully misinformed.

    >delete pCls1;
    This, on the other hand, is very bad. pCls1 points to cls1, so using delete will try to free the memory allocated to cls1. However, since cls1 wasn't allocated using new, the behavior is undefined. With any luck it'll crash immediately and leave a nasty message in your face so that you can fix it.

    Thank you for reply!
    That's right with the error message.

    Ok, pCls1 points to a valid object that has its own memory. Why can not use delete for deleting pCls1? I know, if i using 'new' it works, but what is the background, I can delete only objects stored in heap, right?
    Last edited by djddj; 11-09-2006 at 12:45 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why can not use delete for deleting pCls1?
    The simple explanation is that you can't delete memory that you didn't allocate with new.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    check out some tutorials if you need help understanding dynamic memory and when its used
    http://www.cplusplus.com/doc/tutorial/dynamic.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM