Simple question

This is a discussion on Simple question within the C++ Programming forums, part of the General Programming Boards category; PHP Code: struct my_struct {     //stuff }; int main () {     my_struct  var;     //do stuff     var =  my_struct ();  //*** ...

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399

    Simple question

    PHP Code:
    struct my_struct
    {
       
    //stuff
    };

    int main()
    {
       
    my_struct var;
       
    //do stuff
       
    var = my_struct(); //***
       
    return 0;

    Does the line with the asteriks beside it first call the my_struct destructor for the contents of var, and then the my_struct constructor?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Does the line with the asteriks beside it first call the my_struct destructor for the contents of var, and then the my_struct constructor?
    First the constructor for var is called, then the constructor for the temporary is called, then the destructor for var is called and the temporary is assigned to var. Then when main exits that destructor is called.
    *Cela*

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    So as long as the destructor of my_struct takes care of all the memory allocation that might have happened, there won't be any memory leaks doing it this way?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>there won't be any memory leaks doing it this way?
    As far as I can tell with the little example program you gave, but there are a lot of ways to cause a resource leak and watching your destructors won't stop all of them.
    *Cela*

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: Simple question

    Originally posted by pianorain
    PHP Code:
    struct my_struct
    {
       
    //stuff
    };

    int main()
    {
       
    my_struct var;
       
    //do stuff
       
    var = my_struct(); //***
       
    return 0;

    Does the line with the asteriks beside it first call the my_struct destructor for the contents of var, and then the my_struct constructor?
    As far as I can see, you have 1) Default constructor 2) Default constructor for temp object and 3) Assignment operator

    If memory is allocated in the construtor, and the assignment operator doesn't deallocate this memory, assigning your object in this manner will create a memory leak. Especially if you use the implicitly generated assignment operator provided by the compiler, as it just does a bitwise copy of members.

    For example:
    Code:
    class myclass
    {
     public:
        myclass()  : m_a(new int[256]) {};
        ~myclass() { delete[] m_a; };
        myclass &operator=(const myclass &rhs) 
        { 
            delete[] m_a; 
            m_a = rhs.m_a; 
        }
     protected:
        int *m_a;
    }
    That would be a proper definition free of memory leaks.

  6. #6
    leakme
    Guest
    the contents of var are simply overwritten (bitwise copy) unless you override the = operator or provide a copy constructor. memory leaks are definately possible in this situation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21