Destructor problems

This is a discussion on Destructor problems within the C++ Programming forums, part of the General Programming Boards category; Hello, Does anyone have experience with Destructors causing problems? I am trying to make a String class and when i ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    8

    Question Destructor problems

    Hello,

    Does anyone have experience with Destructors causing problems?

    I am trying to make a String class and when i call methods accepting a String object, the program bomb. (VisC++6 bombs but DevC++ Bloodshed is ok).

    If i comment out delete etc in the Destructor, then all is well.

    In main() i declare 2 string objects:

    MyString s1 = "Big DOG;
    MyString s2 = "big dog" ;

    cout << s1.Compare(s2); method shown below / / Sometimes ok but,

    if i do

    S2.getValue(); // returns char* to s2 string.

    this bombs, but s1.getValue() is fine.

    Any input very much appreciated, thanks.



    ******

    int MyString::Compare(MyString S){

    char *cptr = 0;

    cptr = this->cpString;
    int iSLength = 0;
    int iLength = 0;
    iSLength = S.GetLength();
    iLength = this->GetLength();

    if( iLength != iSLength){
    return 0;
    }
    else{
    while(*cptr){

    if(*cptr != *(S.cpString)){
    return 0;
    }
    cptr++;
    S.cpString++;
    }
    return 1;
    }
    }
    // Destructor
    MyString::~MyString(){

    delete [] cpString;
    cpString = 0;
    }
    Last edited by Bozz; 02-25-2003 at 04:52 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    zip up the class into an attatchment and ill be happy to take a look at it.
    One word of advice. I would try not to obfuscate variable names with semi-hungarian notation. Code should be self documenting and hungarian notation defeats that purpose and gives absolutely nothing that any modern ide can give you with a mouseover tooltip.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    hey Bozz..send me it too...i want to help you again
    nextus, the samurai warrior

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Thanks guys, im sending you a zip with my class header file, a cpp file with function definitons and cpp Main test file.

    Hope its not too confusing.
    Appreciate your help,

    bozz

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    ...forgot the zip file...
    Attached Files Attached Files

  6. #6
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    1)
    since cpString = new char[iLength];
    when you call the destructor dont use delete cpString, but
    Code:
    delete [] cpString
    because you are allocating an array of characters not just one

    2)
    you GetLength() member function needs an agrument..if it doesnt get an agrument then your it wont return a number...so put like GetLength(cpString);

    etc..etc..the reason you are getting these kinds of errors is because you still dont get pointers

    char* string = new char[15];
    string = "cpp_ninja";

    char* string2 = new char[15];
    string2 = string; //uh-oh i am just setting the address of string 2 to string..once string one destructor dies..string2 points to nothing

    anyways i fixed your code..it was dirty...and still easy i prefer writing code like
    Code:
    int main()
    {
       if(any condition)
         { // anything goes
         }
       return 0;
    }
    download the attach file..your thing works now..its 1:20 am here..i am just downloading and watching subbed anime...grrr...i have no LIFE
    Attached Files Attached Files
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

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