Thread: Classes, dynamic variables, and leaks.

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Classes, dynamic variables, and leaks.

    If I create an instance of a class, and then delete it, I assume that the non-dynamic variables are all cleaned up automatically. The destructor is supposed to be used for dynamically allocated variables, right?

    If I use dynamic pointer to create a class, I am assuming that the non-dynamic variables within the class will still be destroyed implicitly, correct? If a dynamic value is assigned within a class, and not handled by the destructor, that would, if I'm not mistaken, create a leak would it not?

    I am just trying to check my understanding to make sure I was up to speed. Pls tell me if I am mistaken about anything.

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    Code:
    class boop {
      int *a;
      int b;
    public:
      boop() {
        a = new int;
      }
      ~boop() {
        delete a;
      }
    };
    
    ...
    
    boop b1;
    boop *b2 = new boop;
    Okay, when b1 is created, b1.a is allocated anonymous memory and b1.b is given memory from the stack. When b1 goes out of scope, b1.b is automatically destroyed because it is local to b1. b1.a's memory is release in the destructor that's called when b1 goes out of scope. All is well unless something freaky happens like a call to abort before b1's destructor can be called.

    b2 is a pointer that's assigned anonymous memory. When that happens, the same deal as with b1 occurs, but this time, b2 has to be explicitly released for the destructor to be called and the memory for b2.a released.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    A equals new int

    So it IS necessary for the constructor to initialize any dynamic variables internal to the class?

    Also, by the way, b1's *a isn't the same value as b2's *a is it? Or would they be seperate values just like int b? Just making sure I understand the nuances of pointers.

  4. #4
    root
    Join Date
    Sep 2003
    Posts
    232
    >So it IS necessary for the constructor to initialize any dynamic variables internal to the class?
    No, it's just convenient. That way you have the object starting out in a predictable state. If you want you can allocate anonymous memory elsewhere in the class definition, it doesn't matter as long as you don't use the pointer until it has memory assigned to it and you release the memory before the object is completely destroyed.

    >Also, by the way, b1's *a isn't the same value as b2's *a is it?
    Nope, b1 and b2 are two separate objects of the boop class. That means that they each have separate copies of all the variables. So b1.a and b2->a are two different pointers. Remember that the pointers themselves are regular variables that hold an address. Two pointers will be different, but may point to the same memory.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Grins2Pain, I think you should take a look at your book's pointer chapter because you seem not to know them well.
    Pointers are variables so they follow the same rules. Pointers *are* destroyed along with the class but (luckily) the memory they point to isn't.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's about right. Classes are basically just buffers of memory much like structs. when you delete that class, you delete that memory. The dynamic stuff is basically when some sort of pointer or handle lives inside that buffer so you need to obviously check your pointers.

    edit:
    wow too slow.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    They MAY but they wont by default?

    They COULD BE MADE to point to the same location, but as shown they would be seperate?

    Lyx Yeah prolly, sorry, I'll quit making so many posts if it's a problem.

    By the by, do you still need the * for b2->a, like would it actually be b2->*a to get the contents as opposed to the address?

    Also, just to make it crystal clear, the delete a; would indeed free up the memory, as opposed to just ending the function without the delete?

  8. #8
    root
    Join Date
    Sep 2003
    Posts
    232
    >but as shown they would be seperate?
    Yes, you could make them point to the same location thus:
    Code:
    delete b1.a;
    b1.a = b2->a;
    Assuming the members were public instead of private of course.

    >do you still need the * for b2->a
    Yes, but the syntax would be *b2->a.

    >the delete a; would indeed free up the memory, as opposed to just ending the function without the delete?
    Correct. But in this case a is the member of a class, so change "ending the function" to "destroying the object without calling delete".
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  9. #9
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Lyx Yeah prolly, sorry, I'll quit making so many posts if it's a problem.
    Hu? Have I said something?

    By the by, do you still need the * for b2->a, like would it actually be b2->*a to get the contents as opposed to the address?
    Actually, I think b2->*a is the syntax of dereferencing a member pointer with an instance pointer. But I almost never used it so, maybe it's not that.

    edit:
    wow too slow.
    Mom wants me to sleep now so you'll be able to take you time answering. ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-02-2009, 08:57 PM
  2. help understanding free and memory leaks
    By mc61 in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 12:47 AM
  3. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  4. Question about Memory Leaks with *char
    By Diamonds in forum C++ Programming
    Replies: 11
    Last Post: 12-16-2002, 07:00 AM
  5. about memory leaks with this simple program
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 07:19 PM