LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2005, 02:55 AM   #1
pippet
Member
 
Registered: May 2004
Posts: 67

Rep: Reputation: 15
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:perator+= () 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
 
Old 02-01-2005, 03:12 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 02-01-2005, 03:35 AM   #3
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
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";
}
 
Old 02-01-2005, 03:45 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 02-01-2005, 03:58 AM   #5
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
Here is it
string Util::toString(float n) {
char str[1000];
sprintf(str,"%f", n);
return str;
}
 
Old 02-01-2005, 04:29 AM   #6
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
rewrite the return-statement to:
Code:
return string(str);
and see if that helps
 
Old 02-01-2005, 04:31 AM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
or, another version:

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

   ss << n;

   return ss.str();
}
for stringstream you #include <sstream>
 
Old 02-01-2005, 04:52 AM   #8
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
Can u tell me which is right way to pass a string as argument to a function?
by reference or value?
 
Old 02-01-2005, 04:56 AM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Always pass std::string by reference. Pass as const reference if the function is not intended to modify the string.
 
Old 02-01-2005, 05:34 AM   #10
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
That means we should not write a function like
string myfn(string s)
{
........................
.......................
}

and us it as

main()
{
string s;
myfn(s);
}
 
Old 02-01-2005, 06:14 AM   #11
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 02-01-2005, 06:34 AM   #12
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
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;
 
Old 02-01-2005, 06:43 AM   #13
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
No that's fine.
A good rule of thumb is to always initialize variables.
 
Old 02-01-2005, 06:58 AM   #14
pippet
Member
 
Registered: May 2004
Posts: 67

Original Poster
Rep: Reputation: 15
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Quota issue, hard limit doesn't limit users Gratz Linux - Software 2 09-16-2003 07:35 AM
sound length mihman Linux - Software 0 08-21-2003 05:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration