Thread: new and delete in class

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30

    new and delete in class

    Hello,
    i got a problem deleting a former in a class constructor created array in the corresponding destructor (see code below). There is a runtime error which insits that the program was trying to free memory twice, without explicitly deleting the array all works fine, but im not sure if this is the right way.

    Code:
    class allocator{
      double* array;
      allocator(arraysize): array(new double[arraysize]){
      }
      
      //is not working as it is supposed to do, thus not used
      /*
       * ~allocator(){ delete [] array;
       *                 }
       */
    }
    
    int main(){
      allocator a1(5);
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    With a view modifications it works fine
    Code:
    class allocator{
       double* array;
    public:// constructor should be public 
        allocator(arraysize): array(new double[arraysize]){
        }
      
        virtual ~allocator(){  // better make it virtual
            delete [] array;
        }
    };  //semicolon needed
    
    int main(){
      allocator a1(5);
    }
    Kurt

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why virtual?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Elysia View Post
    Why virtual?
    Somebody might have the idea to inherit from that class.
    Kurt

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ...insits that the program was trying to free memory twice
    You were probably making shallow copies of the object.
    Code:
    class allocator
    {
        // prevent copies from being made
        allocator(const allocator&);
        const allocator& operator=(const allocator&);
    
    public:
        double* array;
      
        allocator(size_t arraysize) : array(new double[arraysize]) {}
        ~allocator() {delete[] array;}
    };//allocator
    I tend to keep virtual out of a class that hasn't been designed to be inherited from.

    gg

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's kind of an implementation question. Each one does their own.
    Maybe it's best to put in a comment saying "you can make the destructor virtual in case you intend to derive classes from it [and use polymerization]".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Hi,
    i think Codeplug is right, there are Parts in my Code which actually allow shallow copies.
    Thanks a lot,
    Ben

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Be really careful about assigning objects that have pointers as base members where it actually allocates memory for those pointers.
    Doing a shallow copy in that case usually means trouble, especially if the class also release the memory in the destructor. Consider doing a deep copy or prevent the object from being copied instead.
    Or you can overload the copy constructor or assignment operator and set one of the class's member to NULL so only one object will take responsibility for the memory.
    Implementation choices.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Pretty much all these choices exist in ready packages, though. std::vector for the deep copy, boost::scoped_array for preventing copies, boost::shared_array for sharing a single buffer between instances, and so on.


    By the way, "polymorphism". "Polymerization" is a cool error to make, but an error nonetheless.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by ZuK View Post
    Somebody might have the idea to inherit from that class.
    Kurt
    Read Chapter 50: http://www.ubookcase.com/book/Addiso...3586/main.html

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    By the way, "polymorphism". "Polymerization" is a cool error to make, but an error nonetheless.
    Stupid dictionary of FF2 I blame it on the dictionary!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    Code:
    you wrote a class that does not diffentiate between the member functions and the data members... you should use the member access specifiers...public, private, protected.
    to modify your class do the following:
    
    1. write the constructor and destructor under the public section.
    2. you should write a semi-colon at the end of the class definition.
    
    class allocator
    {
    
    double* array;
    
    public:
    
    allocator(arraysize): array(new double[arraysize])
    {
    }
      
     ~allocator()
    {
     delete [] array;
    }
     
    };
    
    int main()
    {
      allocator a1(5);
    }

Popular pages Recent additions subscribe to a feed