Thread: Get an error when trying to clear the heap space

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    Get an error when trying to clear the heap space

    Hey,
    I am getting a runtime error when I run my removeSomeElement() function. Can someone have a look and tell me what I am doing wrong.
    thanks a lot.

    My Code is:
    Code:
    #include "Person.h"
    class SetOfPersons{  //Unbounded Container 
    public: 
         SetOfPersons(int n = 4) : index(0),size(n){ 
             buffer = new Person*[n]
          ;} 
     
        ~SetOfPersons(void) {delete [] buffer;} 
        
            void add(Person & item) { 
    	 if(index == size) {  //grow the container 
                     cout << "growing\n"; 
                     Person ** temp = buffer; 
                     buffer = new Person*[size*2]; 
                     for(int i=0; i<size; i++) buffer[i] = temp[i]; 
                      size = size *2; 
                      delete [] *temp; 
                     } 
            buffer[index++] = &item; 
         } 
         
          int sizea(){ return index;}
    
         Person & removeSomeElement() { 
             index--; 
             if(index < size/2) {  //shrink the container 
    	 return *buffer[++index];
    	 cout << "shrinking\n"; 
                     Person ** temp = buffer; 
                     buffer = new Person*[size/2]; 
                    for(int i=0; i<index; i++) buffer[i] = temp[i]; 
                       size = size/2; 
                       delete [] *temp; 
             } 
    		 
          }        Person & someElement() {
               if(index>0){
                  return *buffer[++index];
               }
              else{index=0;}
           }
    	 
    
    void printOn(ostream & ostr){ 
    		 
    	 } 
    
    
    private: 
          Person ** buffer; 
          int size; 
          int index; 
    }; 
    ostream & operator<<(ostream & ostr, SetOfPersons & p) { 
         p.printOn(ostr); 
         return ostr; 
    }
    My main file is:
    Code:
    //file main.cpp 
    #include <iostream.h>  
    #include <string.h> 
    #include "Date.h" 
    #include "Person.h" 
    #include "SetOfPersons.h" 
    
    main() { 
    int i;
    //create some persons on the heap
    Person *p1 = new Person("Lou", "[email protected]", 20, 6, 1960); 
    Person *p2 = new Person("Frank", "[email protected]", 20, 3, 1967); 
    Person *p3 = new Person("Ann", "[email protected]", 20, 8, 1960); 
    Person *p4 = new Person(); 
    //Create some sets
    SetOfPersons boys, girls; 
    
    //Add some persons to the sets
    
    boys.add(*p4); 
    
    //test to see if the same object is retrieved from the set. 
    if (p1 != &boys.removeSomeElement() ) 
      cout << "ERROR: the objects are different \n"; 
    else 
      cout << "Good, the objects are the same \n"; 
    
    
    boys.add( *p1); 
    boys.add( *p2); 
    girls.add( *p3); 
    
    boys.add(*(new Person("John", "[email protected]", 20, 3, 1967))); 
    girls.add(*(new Person("Sue", "[email protected]", 20, 3, 1967))); 
    boys.add(*(new Person("Frank", "[email protected]", 25, 4, 1958))); 
    girls.add(*(new Person("Mary", "[email protected]", 25, 4, 1955))); 
    boys.add(*(new Person("John", "johnchat.ca", 12, 12, 1970))); 
    
    
    
    //print all the boys using the removeSomeElement() method and delete them 
    int numberOfBoys = boys.sizea(); 
    cout << "number of boys = " << numberOfBoys << "\n"; 
    for(i = 0; i<numberOfBoys; i++) { 
     Person & boy = boys.removeSomeElement(); 
      //cout << boy << "\n"; 
     // delete &boy;  
      } 
      
    
    //print the girls using the << operator of the SetOfPersons class 
    
    //cout << "number of girls = " << girls.sizaa() << "\n"; 
    //cout << girls << "\n"; 
    
    //print of the girls birthdays and using the someElement() method 
    int numberOfGirls = girls.sizea(); 
    //for(int i = 0; i<numberOfGirls; i++) 
      //cout << girl.someElement().getBirthDate() << "\n"; 
      
    
    //delete all the girls from the heap 
    numberOfGirls = girls.sizea(); 
    for(i = 0; i<numberOfGirls; i++) { 
      Person & her = girls.removeSomeElement(); 
    //  delete &her; 
      
    
    } //end of main 
    
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your removeSomeElement() function doesn't always return a meaningful value.
    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
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    how should I return the meaniful value. Sorry I am new to this and I cannot figuer out the problem. thanks

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Since it doesn't return a value, make the return type void. This is the reason why it's good to explicitly state a return value.
    Code:
     removeSomeElement();
    This function prototype implicitly returns int since that's the compiler's default type, but we want to return nothing, so this doesn't work.
    Code:
    void removeSomeElement();
    Now it returns nothing: void.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    I am returning a value it is " return *buffer[++index];" it's in the second line and not on the end of the function.

    thanks

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    so what does it return when if(index < size/2) turns out to be false?

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    what should I return if this is false.
    if(index < size/2)

    I tried return *this but that does not work.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Maybe you should return a pointer, NULL. If you return a reference it implies that you always have a value to return.

    [edit]
    I tried return *this but that does not work.
    Of course that doesn't work. *this is a SetOfPersons, and the function returns a Person.
    [/edit]
    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.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    I tried that if index<size/2 then return an empty Person Object. That does not seem to work. My code looks like this:

    Code:
     Person &removeSomeElement(){
    		static Person InvalidPerson("","", 0, 0, 0);
    		numberOfElements--; 
             if(numberOfElements < capacity/2) {  //shrink the container 
                cout << "shrinking\n"; 
                Person ** temp = elements; 
                elements = new Person*[capacity/2]; 
                for(int i=0; i<numberOfElements; i++) elements[i] = temp[i]; 
                capacity = capacity/2; 
                delete [] temp; 
             } 
    		 else {
    			return InvalidPerson;
    		}
    	}

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's why I think you should return a pointer instead. You can return NULL to indicate "no object".
    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.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    You were right thanks alot that worked!!. I have one more question in my printOn Method how should I print the elements of the set . Does that mean that I loop through the array and print the elements out.

    Code:
    void printOn(ostream & ostr){ 
     for(int i=0; i<sizea(); i++)
     cout<<buffer[i];
    }
    When I do this I get an error on the //cout << boy << "\n"; line

    Code:
    int numberOfBoys = boys.sizea(); 
    cout << "number of boys = " << numberOfBoys << "\n"; 
    for(i = 0; i<numberOfBoys; i++) { 
     Person & boy = boys.removeSomeElement(); 
      //cout << boy << "\n"; 
     // delete &boy;  
      }

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    cout << object expects the object to have overloaded the << operator. If you use your own method instead of operator<<, you'll need to call it directly.
    Code:
    object.printOn();
    Alternatively, you could make printOn return a string, that you print:
    Code:
    string printOn() {
        return "something";
    }
    
    cout << object.printOn();
    You might want to choose a different name in that case.

    Or you could just overload the << operator. That's pretty hard to do, though. Read up on it under "operator overloading".
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  2. How to Clear in Visual C++
    By MyDestiny in forum Windows Programming
    Replies: 4
    Last Post: 03-16-2005, 10:40 PM
  3. How to Clear in Visual C++ 6.0
    By MyDestiny in forum Windows Programming
    Replies: 1
    Last Post: 03-16-2005, 11:57 AM
  4. stach and heap.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-26-2002, 09:37 AM
  5. error at 0x%08X
    By Bobish in forum C++ Programming
    Replies: 7
    Last Post: 11-06-2001, 05:30 PM