Sohag Mony
Sohag Mony

Reputation: 735

Android application crashing for string manupulation in c++

I am using C++ library using android ndk. In c++ library there are a method which has many string append operation. So in my code i just declare a string object then, operate several append operation on it. In c++ library that method has been called too much. And my android application is crashin in the method. CPU usage is too high. My question is why this is happening? Is it good idea using std reserve operation?

example usage:

std::string GetString() const 
{ 
  std::string str1; 
  str1 = "something"; 
  str1 += "somestring"; 
  if(...) { str1 += "somestring"; str1 += "somestring"; }
  str1 += "somestring"; 
  str1 += "somestring"; 
  str1 += "somestring"; 
  str1 += "somestring"; 
  str1 += "somestring"; s
  str1 += "somestring"; 
  return str1; 
}

And this method is calling 50 or more times from C++ lib. One more thing i should mention. I am getting following crash report signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000010. May be this is a sigmentation fault.

Upvotes: 0

Views: 176

Answers (1)

Balog Pal
Balog Pal

Reputation: 17183

yes, for that usage calling .reserve() is definitely a good idea, it can save many reallocations so run faster and keep the memory less fragmented.

Though I see no other possible reason for a crash than running out of usable memory.

Upvotes: 1

Related Questions