Advertisement

debug help

Started by December 31, 2002 01:55 PM
3 comments, last by Nexus999 21 years, 10 months ago
OS: Windows XP Compiler: MS Visual c++ 6 I have been trying to fully grasp the concept of pointers and their uses. I have written a small class with implamenting code and every time I compile and run it, it goes just to the end of the program then terminates unexpectidly. if some one could please tell me what I am doing wrong, I would be much appericiated. The code: ---------------------------------------------------------------- #include <iostream> #include <string.h> class CTest { private: // char pointer char * string; public: // default constructor CTest (void) { string = new char [33]; *string = ''\0''; } // constructor that takes string and puts it into the pointer string CTest (char NewString [33]) { string = new char [33]; strcpy (string, NewString); } // destructor ~CTest (void) { delete string; } char * GetString (void) { return string; } void PutString (char NewString [33]) { strcpy (string, NewString); } } main () { char NewString [33]; CTest * TestOne = new CTest (); std::cout << "Write a string 32 chars long: "; std::cin >> NewString; TestOne->PutString (NewString); std::cout << "The string you typed is: " << TestOne->GetString () << std::endl << std::endl; std::cout << "Write a string 32 chars long: "; std::cin >> NewString; TestOne->PutString (NewString); std::cout << "The string you typed is: " << TestOne->GetString () << std::endl << std::endl; delete TestOne; return 0; } ---------------------------------------------------------------- Thank you again
-= N E X U S =-
In your CTest class''s destructor you call:

delete string;

it should be:

delete [] string;

This is because you allocated the memory with new [].

-Mezz
Advertisement
Put a ; after your class definition and before main, that was wierd.

[edited by - Skibum on December 31, 2002 3:23:09 PM]
It works fine for me. When you type in 33 or more characters, however, it will crash.
Thanx for the suggestions! It works fine.. and I feel like a complete jackass. Got live and learn i guess. The idea for a string length check was very helpful! Thanx again.

Just for kicks, here is the updated working code:

----------------------------------------------------------------

#include <iostream>
#include <string.h>

class CTest
{
private:
// char pointer member
char * string;

public:
// default constructor
CTest (void)
{
string = new char [33];
*string = ''\0'';
}
// constructor that takes string and puts it into the pointer string
CTest (char NewString [33])
{
string = new char [33];
strcpy (string, NewString);
}
// destructor
~CTest (void)
{
delete [] string;
}

// Misc. methods
char * GetString (void)
{
return string;
}
void PutString (char NewString [33])
{
strcpy (string, NewString);
}
void CheckLength (char NewString [])
{
if ((strlen (NewString)) > 33)
{
std::cout << "Invallid string." << std::endl;
exit (0);
}
}
};

main ()
{
char NewString [33];
CTest * TestOne = new CTest ();

std::cout << "Write a string 32 chars long: ";
std::cin >> NewString;

TestOne->PutString (NewString);
TestOne->CheckLength (NewString);

std::cout << "The string you typed is: " << TestOne->GetString () << std::endl << std::endl;

std::cout << "Write a string 32 chars long: ";
std::cin >> NewString;

TestOne->PutString (NewString);
TestOne->CheckLength (NewString);

std::cout << "The string you typed is: " << TestOne->GetString () << std::endl << std::endl;

delete TestOne;

return 0;
}

----------------------------------------------------------------

Thanx again! -=Nexus=-
-= N E X U S =-

This topic is closed to new replies.

Advertisement