C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 15th, 2006, 01:00 PM
UnderWaterman UnderWaterman is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 UnderWaterman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 30 sec
Reputation Power: 0
Trouble with destructors in class

Ive been working on this code for quite a while and now I come for help with my difficulties .

This is part of the driver program to be used.

Code:
void read_poly( Poly & p )
{
    Term t;
    char ch, var;
    int coef, exp;
    while ( ( ch = cin.get() ) && ( ch != '\n' ) )
    {
        if ( ch == '+' )
        {
            cin >> coef >> var >> exp;
            t.set ( coef, var, exp );
            p += t;
        }
        else if ( ch == '-' )
        {
            cin >> coef >> var >> exp;
            t.set ( -1 * coef, var, exp );
            p += t;
        }
    }
}



This is what I have been encountering problems with.

Code:
// input: A Term t
// output: none
// return: returns a pointer to this
// notes: This function assigns the polynomial the term that was  sent to it.  However, the term that is written to the polynomial overwrites the old term that was stored in the poly(this).

Poly Poly::operator+= (const Term &t)
{
    if(this->used < this->size)
    {
        *this[used].terms = t;  //assigns this the value of t's items
                               // ((doesnt work as expected))
        this->used++;    //increases the amount of terms used
    }
    else
    {
        grow(*this);      //grow the array to a larger size
       *this->terms = t; //same as above
        this->used++;
    }

return *this;
}


/////
This is my grow() function that should(but doesnt necessarily) increase the size of the polynomial by 5 in order to hold additional terms, replace the polynomial with a new poly of size+5, keeping the old terms. This function also should delete [] the old array of polynomials in order to prevent memory leakage.
/////

Code:
 
// input: The Polynomial P gets passed to this function
// output: none
// return: a new array with a new size to allow more elements to be put in
// notes: This function takes an array and expands it by 5 and
//        deletes the old array

Poly *grow(Poly &p)
{
    int newsize = p.size + 5;    //declaring new size of old size + 5
    Poly *narr = new Poly[newsize];//allocates new memory location of newsize
    for(int i = 0;i < p.size;++i)//loop to assigne values
        narr[i] = p;
    cerr << "eh?";
    delete [] &p; //delete old array -- does not work - causes seg fault
    narr->size = newsize;
    return narr;
}



and for my class definitions :

Code:
/////////////////////////////
///Polynomial class definition
//////////////////////////////
class Poly
{
    public:
        Poly(void);     //constructor
        ~Poly(void);    //destructor
        Poly(const Poly &p);
        friend class Term;
        void copy(const Poly &p);  //copy values
        friend Poly *grow(Poly &p);
        Poly operator+= (const Term &t);
        Poly operator+ (const Poly &p)const;
        Poly operator- (const Poly &p)const;
        Poly operator* (const Poly &p)const;
        Poly operator= (const Poly &p);
        friend ostream& operator<< (ostream &streamout, const Poly &p);
       


    private:
        Term * terms;   //Term values
        int used;      //terms in use
        int size;     //size of poly array
};
 


Code:
 
///////////////////////
/////Term class definition
//////////////////////
class Term
{
    friend class Poly;
    public:
        Term(void);
        int getcoef(void)const;
        int getexp(void)const;
        char getvar(void)const;
        void set(int = 0, char = 'x', int = 0);
        friend ostream& operator<< (ostream &streamout, const Term &t);
    private:
        int coef;
        char var;
        int exp;
};


any help as to how to prevent the old terms from being deleted, delete the old array properly in grow function, etc would be greatly appreciated

Reply With Quote
  #2  
Old February 27th, 2006, 01:17 AM
LightPropeller LightPropeller is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 1 LightPropeller User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 33 sec
Reputation Power: 0
Dyn Mem -vs - Vector

This problem of memory allocation and growth appears more readily managed with a vector. you can add terms without concern for lost goodies and you don't have to mangage the memory leak. Otherwise your work looks good to this point.

Reply With Quote
  #3  
Old March 4th, 2006, 11:13 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
i don't know if you can really delete[] addresses-of-references...what if you passed a pointer-to-Poly instead?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Trouble with destructors in class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT