Write my own string class is my assignment and there have some problems i can't figure out why?
here is my code:

//this is one kind of constructor--this one i couldn't change;
String::String(char ch, size_t count /* = 1 */)
{
  memset(m_buffer,ch,count);
  m_length=count;
}
//this is overload operator
String &String::operator = (const String& other) 
{  
  
	if(this->m_buffer!=other.m_buffer)
   {
		if(m_buffer!=NULL)
	   {
		 delete [] m_buffer; 
		 m_length=strlen(other.m_buffer) + 1;
		 m_buffer = new char[m_length];
		 strcpy(m_buffer,other.m_buffer);
		}
		else
		{
		  m_length=strlen(other.m_buffer) + 1;
		  m_buffer = new char[m_length];
		  strcpy(m_buffer,other.m_buffer);
		}
		
      return *this;
   }
   else
	  return *this;

}


String& String::operator = (const char *other)
{
	if(this->m_buffer!=other)
	{ 
		if(m_buffer!= NULL)
		{
		 delete [] m_buffer;
		 m_length=strlen(other)+1;
		 m_buffer=new char[m_length];
		 strcpy(m_buffer,other);
		}
		else
		{
			m_length=strlen(other)+1;
		    m_buffer=new char[m_length];
			strcpy(m_buffer,other);
		}
	
	   return *this;	
	}
	else
		return *this;


}

and the driver:

int main()
{
 String ditty;
 ditty = String('p',0);
}

couldn't pass

if somebody know how to solve this problem, plz help me.

There have one init() function inside the constructor

void String::init()
{
  m_buffer = NULL;
  m_length = 0;
  m_bufSize = 0;
}

So that's "no" then.

Yes it's initialised, but it isn't pointing at any memory you can use.

you have more problems in the assignment constructor. [in addition to horrible code repeat!]
e.g.

m_length=strlen(other.m_buffer) + 1;
m_buffer = new char[m_length];
strcpy(m_buffer,other.m_buffer);

Now consider what happens if other.m_buffer is a null pointer. That is a seg fault.

but i have the
if(m_buffer!=NULL)
??

So you mean my problem is the "overload operator ="
cause i couldn't pass the
ditty = String('p',0);
thx

Salem + Me have pointed out two different points in the code that you have errors:

From you last post:

You have if(m_buffer!=NULL) BUT you don't have if (other.m_buffer!=NULL) . You can get to the line m_length=strlen(other.m_buffer) + 1; with [B]other[/B].m_buffer == 0 That is why THAT piece of code doesn't work.

Then you have to fix the error that Salem pointed out. You have a memset() without allocating the memory which is written to by the memset. memset sets memory it does not allocate memory or check that the pointer passed to it points to suitable memory.

First thx for replying
but i am a little confused.
i don't think i need to judge the other.m_buffer==NULL. Cause i was thinking if other.m_buffer==0 then m_buffer=0 this is what operator "=" did.
then about the memset() part, i couldn't change it cause its already there and it my assignment which didn't allow to change this part.

Well start with the second first. If the memset is in your assignment and you can't add code in the lines above it then 100% of this class can't fix the error.

The first: If you call an operator= what you are doing is getting to say how A=B is done. Therefore what it does is EXACTLY and ONLY what you have in your operator= method.

Therefore, it only does what is in your code, line by line.

String& String::operator = (const char *other)
{
	if(this->m_buffer!=other)
	{ 
		if(m_buffer!= NULL)
		{
		 delete [] m_buffer;
		 m_length=strlen(other)+1;

I have extracted the code and will go through it line by line.
First line is the function entry point, other is set to a value, let us say is 0.

Then we have a test, assume that this->m_buffer=0x4535.
That is not equal to 0. So the test passes.

Then we have another test (line 6) , well 0x4535 is not 0 so that also passes.

The (line 9) you have strlen(other) and strlen(0) is a seg fault.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.