Classes and char* and Dynamic memory allocation

This is a discussion on Classes and char* and Dynamic memory allocation within the C++ Programming forums, part of the General Programming Boards category; Hello- Can anyone please help? THis code compiles fine and bombs just as sweet! i tried debugging, and starting at ...

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

    Angry Classes and char* and Dynamic memory allocation

    Hello-

    Can anyone please help?
    THis code compiles fine and bombs just as sweet!

    i tried debugging, and starting at this line:

    CIS025String s3 = "Hello!";

    i get ERROR cpString, expression cannot be evaluated.

    Any thoughts most appreciated, thanks!


    *************************************************

    #include <iostream>


    using namespace std;

    class CIS025String{

    private:
    char *cpString; // Pointer to store the String Object ( Address of block of memory assigned for the string
    // when the object is created).

    public: CIS025String(); // Default Constructor
    CIS025String(const char *cpS ); // Constructor
    ~CIS025String(); // Destructor

    int GetLength();
    int IsEmpty();

    private: int CIS025String::GetSize(const char *cpS);

    };

    /********************
    * Class CIS025String.
    * Function Definitions
    *
    *
    ********************/



    int main(){

    CIS025String s3 = "Hello!"; // 7 Chars ( 6 chars + '\0' )



    cout<< s3.IsEmpty();


    return 0;

    }

    CIS025String::CIS025String(){ //Default Constructor

    cout << "Using Default Constructor" << endl;

    char *cpString = new char[1];
    *cpString = 0;
    }

    CIS025String::CIS025String(const char* cpS){ // Constructor

    cout << "Using Constructor" << endl;

    int iLength = GetSize(cpS) + 1;

    cout << "String Length = " << iLength-1 << endl;


    char *cpString = new char[iLength];
    *cpString = 0;

    for( int i = 0; i < iLength; i++ ){
    *(cpString+i) = *(cpS+i);
    }
    cout << cpString << endl; // Will output the String.

    }
    CIS025String::~CIS025String(){ //Destructor Declaration

    delete [] cpString;
    cpString = 0;
    }
    int CIS025String::GetSize(const char *cpS) {

    int iLength = 0; // Character Counter

    const char *ptr = 0;
    ptr = cpS;

    while(*ptr){
    iLength++;
    ptr++;
    }
    return iLength;
    }
    int CIS025String::GetLength() { //returns String Length (not inclusding terminating '\0'

    int iLength = 0; // Character Counter

    char *ptr = 0;
    ptr = cpString;

    while(*ptr){

    iLength++;
    ptr++;
    }

    return iLength;

    }
    int CIS025String::IsEmpty() {


    char *ptr = 0;
    ptr = cpString;
    cout << *ptr;
    if(!*ptr){

    return 0;

    }
    else

    return 1;

    }
    ************************************************

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    can you use the cstring header file?

    Code:
    #include <cstring>
    
    int x = strlen(pcharVar);
    strcpy(pcharVar, pFromCharVar);
    must simple...
    nextus, the samurai warrior

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    found your problem!!!!!

    you masked the class variable char *cpString with a local one

    get rid of

    Code:
    char * cpString = new char[iLength];
    
    // and put this
    cpString = new char[iLength];
    here is the working version of it

    Code:
    #include <iostream>
    
    
    
    using namespace std;
    
    class CIS025String{
    
    private:
    char *cpString; // Pointer to store the String Object ( Address of block of memory assigned for the string
    // when the object is created).
    
    public: CIS025String(); // Default Constructor
    CIS025String(const char *cpS ); // Constructor
    ~CIS025String(); // Destructor
    
    int GetLength(); 
    int IsEmpty(); 
    
    private: int CIS025String::GetSize(const char *cpS);
    
    };
    
    /********************
    * Class CIS025String.
    * Function Definitions
    *
    *
    ********************/
    
    
    
    int main()
    {
    	CIS025String s3 = "Hello!"; // 7 Chars ( 6 chars + '\0' )
    
    	cout<< s3.IsEmpty();
    
    	return 0;
    
    }
    
    CIS025String::CIS025String() //Default Constructor
    {
    	cout << "Using Default Constructor" << endl;
    	// doesn't need to allocate memory on the heap
    }
    
    CIS025String::CIS025String(const char* cpS) //Constructor
    {
    
    	cout << "Using Constructor" << endl;
    
    	int iLength = GetSize(cpS) + 1; 
    
    
    	cout << "String Length = " << iLength-1 << endl;
    
    	char * cpString = new char[iLength];  
    
    	for(int i = 0; i < iLength; i++ )
    	{
    		cpString[i] = cpS[i];
    		//cout << cpS[i] << endl;
    	}
    	cout << cpString << endl; // Will output the String.
    
    }
    CIS025String::~CIS025String() //Destructor Declaration
    {
    	delete [] cpString;
    	cpString = 0;
    }
    int CIS025String::GetSize(const char *cpS) 
    { 
    
    	int iLength = 0; // Character Counter
    
    	const char *ptr = cpS;
    
    	while(*ptr)
    	{
    		iLength++;
    		//cout << ptr << endl;
    		ptr++;
    	}	
    	return iLength;
    }
    int CIS025String::GetLength() //returns String Length (not inclusding terminating '\0'
    {
    
    	int iLength = 0; // Character Counter
    
    	char *ptr = 0;
    	ptr = cpString;
    
    	while(*ptr)
    	{
    		iLength++;
    		ptr++;
    	}
    
    	return iLength;
    
    }
    int CIS025String::IsEmpty() 
    {
    	char *ptr = 0;
    	ptr = cpString;
    	cout << *ptr;
    	if(!*ptr)
    	{
    		return 0;
    
    	}
    	else
    	{
    		return 1;
    	}
    
    }
    i know it is syntax correct..but logically it doesnt work..so i fixed it... you're welcome
    nextus, the samurai warrior

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Brilliant!!!
    you are my hero-cant tell you how pleased this makes me.

    It looks so obvious now, and my pc has stopped the BONG noise
    If you are in Berkeley, CA i have to buy you a pint!

    cheers,

    bozz
    Happy Coding

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