0
#include <bits/stdc++.h>
#include <string>

using namespace std;

vector<string> func(string mag) {
  vector<string> s1;
  string temp1;
  string temp2;

  temp1[0] = 'a';
  temp1[1] = 'b';
  temp1[2] = 'c';

  temp2 += "xyz";

  s1.push_back(temp1);
  s1.push_back(temp2);
  return s1;
}

int main() {
  string st;
  vector<string> xyz;
  xyz = func(st);

  for (int a = 0; a < xyz.size(); a++)
    cout << xyz[a] << ',';
}

The output of the above code is ,xyz, even though I was expecting abc,xyz,. The string temp1, added to the vector, is not printed.

The string temp1 is non-empty, and printing it out gives output abc, but pushing it to the vector s1 and printing out the vector, the string temp1 is not printed, while string temp2, where "xyz" is added to it as temp2+="xyz", is added to the vector and is printed when the contents of the vector are printed.

Can someone explain to me why the string temp1 is not appearing in the vector contents?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
adikj
  • 15
  • 4

3 Answers3

2

string temp1; creates an empty string. Accessing any of its elements is an out-of-bounds-access causing undefined behaviour. If you want to access a string's contents like this, resize it before using it:

string temp1;
//    VVVVVV
temp1.resize(3); // now it contains 3 elements

temp1[0]='a'; // set first element, ok
temp1[1]='b'; // set second element, ok
temp1[2]='c'; // set third element, ok
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
1

This code:

string temp1;
temp1[0]='a';  // UB

invokes undefined behavior. temp1 has size 0, so indexing at the first position (index 0) is not allowed (as pointed out in the comments, there is an exception that writing the character '\0' at the size index is allowed).

Instead, add the characters using += or push_back instead. Or resize the string to the appropriate size before indexing into it.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Thanks! But if I print the string temp1 I get 'abc' as the output. If it's not allowed, why does the string have the correct values? – adikj Aug 14 '20 at 16:45
  • 2
    UB means anything can happen. Sometimes the values might appear to be right, but the *entire* program is broken. – cigien Aug 14 '20 at 16:46
  • Ohh. Thanks for clarifying! – adikj Aug 14 '20 at 16:53
  • @cigien "*temp1 has size 0, so indexing at the first position (index 0) is not allowed*" - technically, in C++11 and later, it *IS* allowed to write to the position at `index=size()`, but the only `char` value allowed to be written to that position is `'\0'` otherwise UB occurs. Writing any `char` value to `index>size()` is always UB. – Remy Lebeau Aug 14 '20 at 18:01
  • @RemyLebeau Thanks for the correction, I appreciate it. Edited the answer. I never seem to remember that exception :p – cigien Aug 14 '20 at 18:10
1

There's an error in your code here:

string temp1;

temp1[0]='a'; // Oops - past the end of the string!
temp1[1]='b'; // Oops - past the end of the string!
temp1[2]='c'; // Oops - past the end of the string!

In each of the indicated lines, you're writing past the end of the string, which leads to undefined behavior.

If you want to set temp1 to be "abc", you could try

temp1 = "abc";

or

temp1 += 'a';
temp2 += 'b';
temp3 += 'c';
Dharman
  • 30,962
  • 25
  • 85
  • 135
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Thanks for that ! But if I print string temp1, I get the correct, expected output 'abc' . Why does that happen if it should lead to undefined behaviour? – adikj Aug 14 '20 at 16:47
  • 2
    @adikj Undefined behavior means the code is broken in such a way that the code could do anything. Just because it outputs what you expect does not mean it is not broken. – Martin York Aug 14 '20 at 16:51