0

I run this test:

TEST_F(CHAR_TESTS, wtf){
  char letter[3] = {'A','B','C'};
  char types[2] = {'o','s'};
  char tmp[3];
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
      for(int k=0;k<2;k++){
        tmp[0]=letter[i];
        tmp[1]=letter[j];
        tmp[2]=types[k];
        std::string combination(tmp);
        std::cout << combination << std::endl;
      }
    }
  }
}

For some reason, this print this:

AAo~
AAs~
ABo~
ABs~
ACo~
ACs~
BAo~
BAs~
BBo~
BBs~
BCo~
BCs~
CAo~
CAs~
CBo~
CBs~
CCo~
CCs~

I do not think it is an issue with the printing itself, as I ended up doing this after noticing some tests comparing strings generated from char arrays were not passing, and I could not figure out why. So it feels like indeed the "combination" strings do not end up having the expected content.

The same code in a "regular" executable (not a gtest) print out what is expected (the 3 chars without the weird supplementary chars).

3 Answers 3

5

Unfortunately std::string does not have a constructor that takes a reference to array of char. So you end up invoking the const char* constructor, and this one requires the pointer to point to the first element of a null terminated string. Your char arrays aren't null-terminated, so you end up with undefined behaviour.

You should null-terminate tmp, which you can do by declaring it with one extra character, and setting it to '\0'. You can achieve that like this

char tmp[4] = {};
Sign up to request clarification or add additional context in comments.

3 Comments

Or char tmp[4] = "";
damn, I just read this a little too fast: stackoverflow.com/questions/8960087/… : "the string class has a constructor that takes a NULL-terminated C-string", I skipped the "NULL-terminated part".
You could also use std::string combination(std::begin(tmp), std::end(tmp));
1

The constructor in std::string combination(tmp); only works if tmp is nul terminated. Otherwise the constructor cannot find the length of the string.

However, you can help std::string by explicitly providing the size of the buffer:

std::string combination(tmp, sizeof(tmp));

This constructor is primarilly intended to construct a std::string from a part of a C-style string, but also works if given the full length.

Comments

0

char tmp[3]; is not null-terminated use char tmp[4] = {0};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.