Iam getting the error Heap Corruption Detected if i run the following program. If i comment out the line delete[], iam not getting the error. Also if i change the outTxtStr = new char[outTxtLength + 1] to outTxtStr = new char[50] iam not getting the error. Does anybody have some idea in how to solve the problem? Please help me.

int outTxtLength;
	char *outTxtStr;
	int i,j = 0;

	outTxtLength = inTxt->length();
	outTxtStr = new char[outTxtLength + 1];
	//outTxtStr = new char[50];
	outTxtLength = inTxt->copy(outTxtStr,outTxtLength);
	outTxtStr[outTxtLength] = '\0';

	// Remove spaces between words i.e consecutive spaces
	for( i=0; i < outTxtLength; i++)
	{
		if ( ( outTxtStr[i]==' ' )&&( outTxtStr[i+1]==' ' ) )
		{
			outTxtLength--;
			memcpy( &outTxtStr[i],&outTxtStr[i+1], outTxtLength);
		}
	}

        delete[] outTxtStr;

Hi meens,

you forgot to add type of inTxt in your code block, is it pointer to char.

please explain your problem with exact code.

ganesh.

Sorry. The declaration of inTxt is like this

string *inTxt;

Hi meens,

I am checking your code, when i am running this there is ni heap error...

check it youself.

int main()
{

	  string Txt = "ganesh shanmuga";//its for testing
	  string *inTxt = &Txt;
	  int outTxtLength;
	  char *outTxtStr;
	  int i,j = 0;
	  outTxtLength = inTxt->length();
	
	  //for checking
	  cout << outTxtLength << "\n";

	  outTxtStr = new char[outTxtLength + 1];
	  
	  //outTxtStr = new char[50];
	  outTxtLength = inTxt->copy(outTxtStr,outTxtLength);
	  outTxtStr[outTxtLength] = '\0';
	  
	  // Remove spaces between words i.e consecutive spaces
	  for( i=0; i < outTxtLength; i++)
	  {
		  if ( ( outTxtStr[i]==' ' )&&( outTxtStr[i+1]==' ' ) )
		  {
			  outTxtLength--;
			  memcpy( &outTxtStr[i],&outTxtStr[i+1], outTxtLength);
		  }
	  }
	   
	  //for checking
	  cout << outTxtStr << "\n" << *inTxt;

	  delete[] outTxtStr;
	  return 0;
}

I forgot one thing, its seems your code did not trim the white spaces

Ganesh

Warning: memcpy( &outTxtStr,&outTxtStr[i+1], outTxtLength);

Don't use memcpy to copy 2 memory blocks which overlap, it will lead to undefined behaviour which is different for different compiler, on diff. system. Use memmove instead.

@ Ganesh:
From what I can see the code is supposed to convert multiple spaces in the input string to single spaces! Try adding some extra spaces in your input string!

Looking at the code, it appears to work for short strings with multiple spaces, but I suspect the OP is using the code to remove extra spaces from very long strings. When longer strings are input the undefined behaviour pointed out by xuangcong is kicking in, causing the problem reported by the OP.

Personally, as the input string is a std::string, I'd use the std::library to replace multiple spaces with single spaces.
Off the top of my head, here's one way of doing it:

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

// Replaces multiple spaces in a string with single spaces.
void RemoveExtraSpaces(std::string *inTxt)
{
	// Set up a couple of iterators
	std::string::iterator it = inTxt->begin();     // this will point to our position in the string
	std::string::iterator endPos = inTxt->end();   // this points to the end of the string

	// iterate through the string
	for( ; it!=endPos; ++it)
	{
		if(*it==' ' && *(it+1)==' ')
		{
			// The characters at the current iterator position
			// and at the next position are both spaces,
			// so remove the current character
			inTxt->erase(std::remove(it, it, ' '), it+1);

			// move the iterator back to compensate for the space character we just removed
			--it;
		}
	}
}

int main()
{
	// Create a std::string
	std::string *myTxt = new std::string("This  is  a  much    longer  string  with  lots   of    spaces!");
	
	// Lets take a quick look at the string
	std::cout << "Before removing double spacing the string reads:" << std::endl << *myTxt << std::endl << std::endl;
	
	// Now call the function, passing our string
	RemoveExtraSpaces(myTxt);
	
	// Has the function removed all of the extraneous spaces?? Lets see!
	std::cout << "After removing the extraneous spaces the string reads:" << std::endl << *myTxt << std::endl << std::endl;

	return 0;
}

Hello Friends,
Thanks for your support.I solved the problem by changing the logic and code for trimming the spaces.I removed the memcpy and used for loop.Now the error is not coming.

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.