Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regarding String.

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
Hi,
I am having a variable of type string (string m_str). The problem is it is able to add to m_str till the length of m_str reaches 300 / 400 and beyond that the program hangs. I wants to store more than 1000 / 2000 characters. I tried resize (m_str.resize()) but this works at some places and does not work at some places. When ever i say m_str="AA" then the length of string will become 2. What could be the problem. Is there is any solution for this problem. Why it is not able to add beyond 300 / 400 characters.

Thank you in advance.
Waiting for the reply.

With Regards,
Kathyayini.

*PP
 
Does this work
Code:
#define BIGSIZE 2000
char* big = new char[BIGSIZE + 1];
memset (big, 'x', BIGSIZE);
big[BIGSIZE] = '\0';
string astr = big;
cout << m_str.size () << endl;
Copy the code segment and put it in various points in your code. Sounds like you have heap corruption somewhere. It will fall over when there is corruption.
 
In all known C++ implementations a string object max length is much more than 300/400. The problem is out of class string size issue...
 
StringUtil& StringUtil::eek:perator=(const StringUtil& src)
{
m_str.resize(src.m_str.length()+10);
m_str = src.m_str; //This gives problem
m_initialized = src.m_initialized;

return *this;
}
In the above mentioned function i am resizing m_str to src.m_str.length()+10 but still it gives problem (programs hangs at m_str = src.m_str;).
 
If your string is a common std::string, there are no any problems (hangs etc) with that assignment. There are another problems:
1. Right pattern for an user defined assignment operator is:
Code:
Type& operator =(const Type& src)
{
  if (this != &src)
  {
  // target != source, do it...
  }
  return *this;
}
2. What for resize at all? See:
Code:
  m_str.resize(NewSize); // Well, resized, but what for?..
  m_str = src.m_str;     // Now m_str has a new value...
stl::string operator =() works fine w/o your resizing help.

It seems a source of your troubles is not that assignment.
May be another ops of StringUtil...
 
i tried with std::string and removed all resizing, but still giving the same problem.
 
Then the source of the problem is in more wide context: StringUtil constructor(s) etc. You MAY store more than 10000 chars in std::string object! It seems you must get more snippets to localize the critical point of...
 
Below is the code of StringUtil.cpp and StringUtil.h


//******* STRINGUTIL.CPP *************

#include "StringUtil.h"
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <tchar.h>
#endif
#include <string>


StringUtil::StringUtil()
{
m_initialized = true;
}

StringUtil::StringUtil (const long int& src)
{
char *buf =new char[MAX_STRING_SIZE];
sprintf(buf,"%ld",src);
m_str = buf;
m_initialized = true;
delete buf;

}

StringUtil::StringUtil (const string& src)
{
m_str = src;

}
StringUtil::StringUtil (const char* src)
{
string tmpStr(src);
m_str = tmpStr;
}

StringUtil::StringUtil(const StringUtil& objStringUtil)
{
m_str = objStringUtil.m_str;
m_initialized = objStringUtil.m_initialized;
}

StringUtil::~StringUtil()
{
}

long StringUtil::getLength() const
{
return (m_str.length());
}

StringUtil& StringUtil::eek:perator=(const char* str1)
{

string tmpStr(str1);
m_str = tmpStr;
return (*this);
}


StringUtil& StringUtil::eek:perator=(const StringUtil& src)
{
m_str = src.m_str;
m_initialized = src.m_initialized;

return *this;
}

StringUtil operator + (const StringUtil& str1, const StringUtil& str2)
{
StringUtil tmpStr;

tmpStr.m_str = str1.m_str + str2.m_str;

return tmpStr;
}

StringUtil operator+ (const StringUtil& s1, const long int& s2)
{
StringUtil tmpStr;

char *buf =new char[MAX_STRING_SIZE];
sprintf(buf,"%ld",s2);
tmpStr.m_str = s1.m_str + buf;

delete buf;

return tmpStr;
}

int StringUtil::extractString(int pos1, int pos2, StringUtil& result)
{
// Errinfo status = SUCCESS;

const int incr1 = pos1;
const int incr2 = pos2;

string oldStr = (*this).m_str;
string newStr = oldStr.substr(incr1, incr2);

result.m_str = newStr;

return 0;// SUCCESS;

}

int StringUtil::findAndReplace(const StringUtil& pattn, const StringUtil& repl, bool bFindOnly, StringUtil& result)
{
// Errinfo status = SUCCESS;

// unsigned int iPos;

if( bFindOnly == false ) {

int ilen = pattn.getLength();
int iReplen = repl.getLength();
int i = m_str.find(pattn.m_str);


while ( i >= 0 ) {

m_str.replace(i,ilen,repl.m_str);
i = m_str.find(pattn.m_str,i+iReplen);

}
}
result.m_str = m_str;

return 0;// status;

}

int StringUtil::compare(const StringUtil& str1, const StringUtil& str2) const
{
if(!str1.m_str.compare(str2.m_str))
return 1;

return 0;

}

StringUtil StringUtil::trim()
{
int nPos = m_str.find_first_not_of(" ");
if(nPos >=0) {
m_str = m_str.substr(nPos,m_str.length());

int oPos = m_str.find_last_not_of(" ");
if(oPos>=0)
m_str = m_str.substr(0,oPos+1);
}

return (*this) ;

}

StringUtil StringUtil::toUpper()
{
int length = (*this).getLength();

for(int index=0;index<length;index++)
m_str[index] = toupper(m_str[index]);

return (*this);
}

int StringUtil::fillData(int iPos,char* cTemp)
{
int iStrLen = m_str.length();

if (iStrLen > iPos)
{
m_str.replace(iPos, strlen(cTemp),cTemp);
return 0;
}
while(iStrLen < iPos)
{
m_str.insert(iStrLen," ");
iStrLen++;
}
m_str.insert(iPos,cTemp);

printf("After Insert Value\n");
return 1;
}

int StringUtil::fillRepeatData(int iPos,char cTemp,int iCount)
{
char cTmp[3];

cTmp[0] = cTemp;
cTmp[1] = '\0';
int iStrLen = m_str.length();
if(iStrLen < iPos)
{
while(iStrLen < iPos)
{
m_str.insert(iStrLen," ");
iStrLen++;
}
}
while(iStrLen < iCount+iPos)
{
m_str.insert(iStrLen,(char*)cTmp);
iStrLen++;
}
return 1;
}

void StringUtil::retrieveData(int iPos, int iLen, char *pcVal)
{

m_str.copy(pcVal,iLen,iPos);
//size_type copy(charT* buf, size_type n, size_type pos = 0) const

}









//************** STRINGUTIL.H ********************

#ifndef STRING_UTIL_H
#define STRING_UTIL_H


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

#define MAX_STRING_SIZE 50

class StringUtil {

public:
StringUtil();

StringUtil(const string&);
StringUtil (const char* ); // constructor

StringUtil (const long int&);


StringUtil(const StringUtil&);

~StringUtil();

const char* getString() const;

long int getLength () const;

StringUtil& operator= (const char* ); // assignment from ccharp
StringUtil& operator= (const StringUtil&); // assignment from StringUtil
StringUtil& operator= (const long int&); // assignment from an Int object
bool operator!= (const StringUtil& s) const;


// extracting strings given the from position(pos1) and the
// to position(pos2)
int extractString(int pos1, int pos2, StringUtil& result);
// find if the current object contains a certain string.
// if the third parameter is set to true, it will only try to find if the
// string exists
// If the third parameter is set to false, it will find and also replace the
// string with the replacement string
int findAndReplace(const StringUtil& pattn, const StringUtil& repl,bool bFindOnly, StringUtil& result);

//compares two string objects and returns TRUE or FALSE
int compare(const StringUtil& str1, const StringUtil& str2) const;

StringUtil toUpper() ; // return lowercase value

StringUtil trim(); // return string without both leading & trailing spaces

int fillData(int iPos,char* cTemp);//similiar like the SetAt in CString
int fillRepeatData(int iPos,char cTemp,int iCount); // Inserts a charecter a specified number of times.
void retrieveData(int iPos, int iLen, char *pcVal); // acts like substr


friend StringUtil operator+ (const StringUtil& s1, const StringUtil& s2);

friend StringUtil operator+ (const StringUtil& s1, const long int& s2);

bool operator< (const StringUtil& s) const;
bool operator> (const StringUtil& s) const;
bool operator<= (const StringUtil& s) const;
bool operator>= (const StringUtil& s) const;
bool operator==(const StringUtil&);
bool operator==(const char* s2);



virtual void display (ostream& oStr) const; // used by the << operator.

friend ostream& operator<< ( ostream& oStr, const StringUtil& str1 )
{
str1.display(oStr);
return oStr;

}
bool is_initialized() const;

private:

string m_str;
bool m_initialized;


};

inline bool StringUtil::eek:perator<(const StringUtil& s) const
{
return(m_str < s.m_str);
}

inline bool StringUtil::eek:perator>(const StringUtil& s) const
{
return(m_str > s.m_str);
}

inline bool StringUtil::eek:perator<=(const StringUtil& s) const
{
return(m_str <= s.m_str);
}

inline bool StringUtil::eek:perator>=(const StringUtil& s) const
{
return(m_str >= s.m_str);
}

inline bool StringUtil::eek:perator== (const StringUtil& str)
{
return(!m_str.compare(str.m_str));
}
inline bool StringUtil::eek:perator==(const char* s2)
{
string tmpBuffer(s2);
return(!m_str.compare(tmpBuffer));
}

inline bool StringUtil::eek:perator!=(const StringUtil& s) const
{
return(m_str.compare(s.m_str) != 0);
}

inline bool StringUtil::is_initialized() const
{
return (m_initialized);
}

inline void StringUtil::display(ostream& oStr) const
{
oStr << m_str << endl;
}

inline const char* StringUtil::getString() const
{
return m_str.c_str();
}

#endif

 
Strictly speaking, I have used VC++ 6.0, not an Unix compiler...
1. There are some syntax errors in the snippet above (unbalanced braces, for example?). Well, corrected...
2. I can add 1000 or more chars to StringUtil objects. More precisely, only std::string limitation applied (check up your compiler specifications). I can't imagine C++ implementation with 255 or near string::max_size(). Call this member function and check up the current string size limitations.
3. Declare more "classic" operator +() members (not friends):
Code:
StringUtil StringUtil::operator +(const StringUtil&) const;
[/code}
4. Your operator << calls StringUtil::display() and insert endl into the stream. Why?..
5. MAX_STRING_SIZE 50 macro? Buffer size for long type text representation is not "max_string_size", rename it. Don't use heap allocation (declare simplest auto char array). Use operator delete [] p (not delete p) to deallocate arrays.
6. You forget to set properly your m_initialized member (in most places). I think, it's useless member: for example, your default constructor set true (but no any value except null string member in that case)...
7. We have a good illustration of well known thesis: std::string class is not dedicated as a root of inheritance tree. It's well-to-do "value", finite class. I can't see any its advantages over std::string. It's better to add simple (free) functions trim(string&) etc...

Grand total:
1. I can't reproduce any hang or exception conditions with your StringUtil (it works fine;).
2. Can your run hang without a successive compilation?
3. Can you show me a hanged code snippet?..
 
The same code works fine at my office and giving problem at clients place. Is the problem is comming because of UNIX environment or something like that.

Below is the code where program hangs(stops) and no further execution (it executes fine for first 3-4 times). After 5-10 minutes if i execute it works fine for 3-4 times and then again hangs and same story continues.

int StringUtil::fillData(int iPos,char* cTemp)
{
int iStrLen = m_str.length();

if (iStrLen > iPos)
{
m_str.replace(iPos, strlen(cTemp),cTemp);
return 0;
}
while(iStrLen < iPos)
{
m_str.insert(iStrLen," ");
iStrLen++;
}
m_str.insert(iPos,cTemp); ////Stops here
return 1;
}



 
I think string system functions (m_str.insert().... m_str.append...... etc) behaves differently in itanium server. Still not getting the solution.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top