Execution error

Aug 2, 2012 at 9:19pm
Hi,

I'm getting the following error when running a c++ code:

terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create

My code is too large to put here (3319 lines) and I have no idea what's causing this. I'll wait for your reply to guide me about what I have to do.

If it helps, I'll put here the thing I think can be related with the error, a function declaration with too much arguments:

 
void getCovariance(std::vector<double>& sigx1vec,std::vector<double>& sigx2vec,std::vector<double>& sigx3vec,std::vector<double>& sigy1vec, std::vector<double>& sigy2vec,std::vector<double>& sigy3vec,std::vector<double>& cov1vec,std::vector<double>& cov2vec,std::vector<double>& cov3vec,std::string& tag,const unsigned int& n,double& x1min,double& x1max,unsigned short& x2min,unsigned short& x2max,double& x3min,double& x3max,double& x4min,double& x4max,double& x5min,double& x5max,double& x6min,double& x6max,double& x7min,double& x7max,double& x8min,double& x8max,TTree *obj, Int_t& n_obj, Myclass *myObject, double& var1min,unsigned short& var2min,double& var3max,double& var4min,double& var5min,double& var6max,double& var7max,double& var8min)                           


The program "ends" while executing this function.

Thank you in advance.
Aug 2, 2012 at 9:36pm
Whilst that function is in serious need of rewriting, you've probably got a string of bad size hanging around. Possibly you've tried to make one simply too large.

Do you not have a debugger you can run this under? It'll give you the exact line causing the problem and show you the function calls causing it.

Aug 2, 2012 at 10:16pm
holyshitmygawdpants

record for most parameters in a function? a get function at that!
Aug 3, 2012 at 3:36am
Whilst that function is in serious need of rewriting,

Yes, you are right! But before rewriting all the function I searched the internet to figure out if the problem was related with the number of arguments in the function definition, and I found that c++ supports up to 256 parameters (I don't know if I can link this part of documentation here because it's an external website).

you've probably got a string of bad size hanging around. Possibly you've tried to make one simply too large.

You are right again, it involves strings. The problem is that I was wrongly using the function string::compare. I'll put here what happened (it can help people with same error):

wrong
1
2
3
std::string mystr = "s1" ;

mystr.compare("s2") ;


right
1
2
3
4
std::string mystr = "s1" ;
std::string anotherstring = "s2" ;

mystr.compare(anotherstring) ;
.

Do you not have a debugger you can run this under? It'll give you the exact line causing the problem and show you the function calls causing it.

Unfortunately I don't have a debugger like gdb because of the kind of environment that I'm using.

Thank you all.
Aug 3, 2012 at 4:29am
closed account (o1vk4iN6)

1
2
3
std::string mystr = "s1" ;

mystr.compare( "s2" ) ;


==

1
2
3
std::string mystr = "s1" ;

mystr.compare( std::string( "s2" ) ) ;


==

1
2
3
4
5
6
7
8
std::string mystr = "s1" ;
std::string anotherstring = "s2" ;
//or
//std::string anotherstring = std::string( "s2" );
//or
//std::string anotherstring( "s2" );

mystr.compare(anotherstring) ;



They all do the nearly identically the same thing. So how exactly is it wrong ?
Last edited on Aug 3, 2012 at 4:32am
Aug 3, 2012 at 7:48am
if s2 is a variable, then he used it in a string, in which case it wouldn't use it as a variable

1
2
3
std::string mystr = "s1" ;

mystr.compare( "s2" ) ; // string is "s2" 


!=

1
2
3
4
//s2 declared and defined somewhere...
std::string mystr = "s1" ;

mystr.compare(s2 ) ; // string is the value of the variable s2 
Last edited on Aug 3, 2012 at 7:49am
Aug 3, 2012 at 12:08pm
xerzi, what I mean in that code is exactly what Zephilinox said. It's different if we pass to function string::compare a string by value rather than by reference. The definitions of string::compare are (cpp reference):

1
2
3
4
5
6
int compare ( const string& str ) const;
int compare ( const char* s ) const;
int compare ( size_t pos1, size_t n1, const string& str ) const;
int compare ( size_t pos1, size_t n1, const char* s) const;
int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const;
int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const;


I think the correct way is to pass a string by reference (although I tried to pass a string by value and it worked fine outside the environment I said in my first post).

passing argument by value (I tried it in a tiny c++ code and it worked, although I think it's wrong)

1
2
3
std::string mystr = "s1" ;

mystr.compare("s2") ;


passing argument by reference (obeys function definition)

1
2
3
4
std::string mystr = "s1" ;
std::string anotherstring = "s2" ;

mystr.compare(anotherstring) ;


Sincerely I don't know if it's correct to pass a string by value. If it follows, the error that I got can be a bug of the framework I 'm using.
Aug 3, 2012 at 1:16pm
mystr.compare("s2") ; executes int compare ( const char* s ) const;
That is not the error, you didn't fix it.

¿Why don't just use == ?


Unfortunately I don't have a debugger like gdb because of the kind of environment that I'm using.
That's simple, change your environment.
Aug 3, 2012 at 2:01pm
mystr.compare("s2") ; executes int compare ( const char* s ) const;
That is not the error, you didn't fix it.

Well, I didn't know that this implementation is valid (now I know, thanks!). In my case, I changed the

mystr.compare("s2") ; // int compare ( const char* s ) const; , as you said

by

mystr.compare(anotherstring) ; // int compare ( const string& str ) const;

and the error message disappeared. In this case, the error probably (almost surely) come from the environment that I'm using, and not from the way I was using the function.

That's simple, change your environment.

I can't. It's an integrated framework used in a physics software that I have to use. There's no way to avoid it.
Aug 3, 2012 at 4:24pm
mystr.compare("s2") ; executes int compare ( const char* s ) const;

I think I found the error, and it can be related with this (char pointer). I defined a function like this:

1
2
3
4
std::string tCombination ;
void setCombination(std::string combination){
   tCombination=combination; // tCombination is a member of the same class as setCombination, global scope
} 


but I was using that function like this:

setCombination("comb1") ;

If we look at your post, I think I was passing a char pointer instead of a string object.
Aug 3, 2012 at 6:41pm
A char pointer will be converted to a string object.
The convertion could fail, by instance if there is no terminating character or if you've got no memory.
Aug 5, 2012 at 8:06pm
The convertion could fail, by instance if there is no terminating character or if you've got no memory.


I changed the code. Now it looks like this:

Red Hat Enterprise Linux 64bit machine
1
2
std::string comb1("comb1");
setCombination(comb1) ;


Ok. I'm running void setCombination(std::string combination) on a Red Hat Enterprise Linux 64bit machine and storing tCombination in a file (specific file to store data). The error appears when I execute the following function (Debian Linux 32bit machine) that reads that content, like this:

std::string getCombination(){return tCombination;}

Debian Linux 32bit machine
1
2
3
4
5
6
7
8
9
std::string tCombination = getCombination() ;
std::string var("comb1") ;

tCombination.compare(var) //error

const char *tCombinatioN =  tCombination.c_str() ;
const char *vaR = var.c_str() ;

strcmp(tCombinatioN,vaR) //works fine 


The problem with memory occurs because I'm running portions of code on different machines?
Aug 5, 2012 at 11:21pm
I said that because though that you were working with really low memory.
but that doesn't seem the case.

About your last post, I'm confused. ¿Are you working in a cluster?
Or you are saying that it works in 64 but not on 32.

¿Could you upload your code to github?
Topic archived. No new replies allowed.