LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   is there any limit for string length in basic_string? (https://www.linuxquestions.org/questions/programming-9/is-there-any-limit-for-string-length-in-basic_string-284795/)

pippet 02-01-2005 02:55 AM

is there any limit for string length in basic_string?
 
Hi,
My c++ project crashes when the processed string length is around 480.
It always crashes at += operation of string. the back trace is


#0 0x001956ad in malloc_consolidate () from /lib/tls/libc.so.6
#1 0x00194caa in _int_malloc () from /lib/tls/libc.so.6
#2 0x0019409d in malloc () from /lib/tls/libc.so.6
#3 0x0083c3ae in operator new () from /usr/lib/libstdc++.so.5
#4 0x008286f9 in std::__default_alloc_template<true, 0>::allocate () from /usr/lib/libstdc++.so.5
#5 0x0082e0f8 in std::string::_Rep::_S_create () from /usr/lib/libstdc++.so.5
#6 0x0082e1ce in std::string::_Rep::_M_clone () from /usr/lib/libstdc++.so.5
#7 0x0082bded in std::string::reserve () from /usr/lib/libstdc++.so.5
#8 0x0082c154 in std::string::append () from /usr/lib/libstdc++.so.5
#9 0x0082bfc6 in std::string::operator+= () from /usr/lib/libstdc++.so.5
#10 0x080abd76 in CEnglishSpeech::convertWords (this=0x9e5f608, words=@0xfefb5d40)
at BhaashanCore/bhaashan/english/CEnglishSpeech.cpp:720
#11 0x080a76d3 in CEnglishSpeech::translate (this=0x9e5f608, text=
{static npos = 4294967295, _M_dataplus = {<std::allocator<char>> = {<No data fields>}, _M_p = 0x9f3b78c "bank.\n\n"}, static _S_empty_rep_storage = {0, 0, 434, 0}}) at BhaashanCore/bhaashan/english/CEnglishSpeech.cpp:179
#12 0x080ec69a in Bhaashan::convertTextToPhonemeSet (this=0x9e5f5a8, text=@0xfefb62b0, language=0x9e85418,
withEmotion=true, withAccent=false) at BhaashanCore/bhaashan/Bhaashan.cpp:1023
#13 0x080eb5c7 in Bhaashan::convertTextToPhonem (this=0x9e5f5a8, text=@0xfefb62b0, withEmotion=true, withAccent=false,
onlyTranscription=false) at BhaashanCore/bhaashan/Bhaashan.cpp:875
#14 0x080eb6b7 in Bhaashan::speakText (this=0x9e5f5a8, text=@0xfefb62b0, withEmotion=true, withAccent=false)
at BhaashanCore/bhaashan/Bhaashan.cpp:891
#15 0x080f9030 in speakText (text=@0xfefb62b0, withEmotion=true, withAccent=false) at BhaashanCore/CBhaashan.cpp:599
#16 0x080faab9 in main () at Example1.cpp:83

what is the maximum length of std::string?
Or the problem occurs some where else?
reagrds,

pippet

Hivemind 02-01-2005 03:12 AM

I don't understand why you just posted debug output and not a small (preferrably complete, without non-standard code) code snippet exhibiting the problem. But to answer your question: std::string can handle strings that size and much longer. It will only fail if the operating system fails to acknowledge requests for more memory.

pippet 02-01-2005 03:35 AM

sorry,
Actually my code is pretty large in size, contained in several files.
I am doubtful that the problem is not with the length, that's why i posted the back trace report. Also, when the length of the string varies, the crashing point also changes with in a particular function,but always to another += operator function.

try
{
string dur=Util::toString(duration);
result += dur; --------------> this gives error
result += tpich.c_str(); //pitch data is at the 50% position
}
catch(length_error &ler)
{

cout << "length error at convert 3";
}

Hivemind 02-01-2005 03:45 AM

Often when trying to create that small code-snippet exhibiting the problem you find the cause and the solution for the problem. The code snippet you posted won't tell us much because there are several things we don't see. Just one thing:
what is the return type of
Code:

Util::toString()
?
You may want to check so you're not returning a reference to an automatic variable.

pippet 02-01-2005 03:58 AM

Here is it
string Util::toString(float n) {
char str[1000];
sprintf(str,"%f", n);
return str;
}

Hivemind 02-01-2005 04:29 AM

rewrite the return-statement to:
Code:

return string(str);
and see if that helps

Hivemind 02-01-2005 04:31 AM

or, another version:

Code:

string Util::toString(float n)
{
  stringstream ss;

  ss << n;

  return ss.str();
}

for stringstream you #include <sstream>

pippet 02-01-2005 04:52 AM

Can u tell me which is right way to pass a string as argument to a function?
by reference or value?

Hivemind 02-01-2005 04:56 AM

Always pass std::string by reference. Pass as const reference if the function is not intended to modify the string.

pippet 02-01-2005 05:34 AM

That means we should not write a function like
string myfn(string s)
{
........................
.......................
}

and us it as

main()
{
string s;
myfn(s);
}

Hivemind 02-01-2005 06:14 AM

No, you should:

Code:

string myfn(const string& s)
{
  /* code here */
}

int main() /* Note: main returns int and its return type is not implicit */
{
  string s;

  myfn(s);

  return 0;
}

And if myfn() needs to change to contents of s the parameter shouldn't be const but still a reference of course.

pippet 02-01-2005 06:34 AM

What happens if i initalize a string like
string s="";
is there any problem with that?
i require it because, i have to do
s+=anotherstring;

Hivemind 02-01-2005 06:43 AM

No that's fine.
A good rule of thumb is to always initialize variables.

pippet 02-01-2005 06:58 AM

The code has extensively used functions having arguments of type string (like myfn)
I think that's why the suggested ways didn't work.
Actually i am porting some code that was written for windows.

It is said that malloc_consolidate() gives segmentation fault when some allocated memory is corrupted by another out of bound memory write. Can u tell me if such a case will occur for the 'myfunction' kind of code?


All times are GMT -5. The time now is 09:52 AM.