2
#include <iostream>
using namespace std;
int main() {
   string s,s_new;
   cin>>s;
   int len=s.length();
   cout<<len<<"\n";
   for(int i=0;i<len;i++){
     s_new[i]=s[i];
   }
   cout<<s[len-1]<<"\n";
   cout<<s_new[len-1];
   return 0;
} 

I am trying to copy string 's' to another string 's_new'. String 's' is taken as an input from user.The code snippet outputs the string length for reference and the other two lines prints the last character from 's' and 's_new'

But for a particular length this program is creating segmentation fault in various IDE. For example, I got segmentation fault for length 25 in Ideone. Also in onlineGDB I got segmentation fault for length 1961.These lengths were not always constant in these IDE.

I was only using lower_case alphabetical characters for these strings and i used randomly generated strings of various lengths for these tests.

I didnot receive any error when I used character arrays for the same job.

I wanted to know if this issue was because of the code or If there is some problem in using the string STL like I did ?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

1 Answers1

1

s_new[i]=s[i]; has undefined behavior since s_new.size() == 0.

You need to resize it to do what you are doing:

s_new.resize(len);

for(int i=0;i<len;i++){
    s_new[i]=s[i];
}

for a particular length this program is creating segmentation fault in various IDE

Writing out-of-bounds always has undefined behavior. In this case, what happens is most likely this:

A std::string often uses small string optimization where the complete string data is stored internally in the std::string. For longer strings a pointer to the string data is used instead (but the user of the class won't notice this). When you do s_new[i] = ... and the string length is 0 and you pass the small string optimization threshold, you start overwriting other internal data in std::string or something else in the memory stored after the std::string.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    Thanks for your answer. It cleared my confusion. Now I understand why it worked when I used a character array instead because it had a specified size. – Shridhar Thakur Jul 21 '20 at 09:58