0

I noticed that if my string is empty, the char array does not become empty either. See the below code:

std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername);

sRecord.m_strUsername is a char[1064], but if it is already populated and i try doing a copy (or strcpy) with strUsername as empty, nothing happens, even though I want sRecord.m_strUsername to become empty or just become whatever strUsername is.

0

2 Answers 2

4

You forgot the zero terminator:

std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername);
sRecord.m_strUsername[strUsername.size()] = '\0';

or alternatively

std::strcpy(sRecord.m_strUsername, strUsername.c_str() );

or if you want all "unused" characters of your array to be \0:

std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername);
std::fill(sRecord.m_strUsername + strUsername.size(), 
          sRecord.m_strUsername + 1064, '\0');

The latter is usually not necessary since C-style strings are commonly defined as a range like [sRecord.m_strUsername[0], first '\0' in the array). What comes after the first \0 usually does not matter unless you want to access that for whatever reason.

The range [str.begin(), str.end()) only contains all the characters of the string. A C-style string (aka array of char) requires an additional '\0'-termination character.

With my first and second proposed fix, the range [sRecord.m_strUsername[0], first '\0' in the array) will be equal to the characters in strUsername as long as the latter does not contain any additional '\0's (something you just cannot easily deal with with C-style strings).

With my third proposed fixed, every character in the array after all the characters from your string will be \0.

Important note: Before doing any of the above, you need to assert that your string contains at most 1063 characters! If this is not certain, raise some kind of error if the assumption does not hold or use std::strncopy instead. strncopy will basically behave like my third snippet, but you have to zero the last element of your array yourself.

Sign up to request clarification or add additional context in comments.

6 Comments

That didn't work. (gdb) p sRecord.m_strSSOToken $25 = "QVFJQzV3TTJMWTRTZmN4MEFSNkljSjBxMXdiMVUvRmg5dS95NytZWEI4TExWU1U9QEFBSlRTUUFDTVRBQUFsTkxBQk00TWpnMk5UTTJPRFExTXpRMU1UYzBPREUzQUFKVE1RQUNNRGc9Iw==", '\000' <repeats 111 times> After the sRecord.m_strUsername[strUsername.size()] = '\0'; where strUsername is empty becomes: (gdb) p sRecord.m_strSSOToken $27 = "\000VFJQzV3TTJMWTRTZmN4MEFSNkljSjBxMXdiMVUvRmg5dS95NytZWEI4TExWU1U9QEFBSlRTUUFDTVRBQUFsTkxBQk00TWpnMk5UTTJPRFExTXpRMU1UYzBPREUzQUFKVE1RQUNNRGc9Iw==", '\000' <repeats 111 times>
@suzanaydin Then you need to be more clear what you want to do. Edit your question to clarify. The code I posted does what I thought you were trying to do.
Sorry for not being clear. Basically, I want the char array to mirror the string in every way. So if the string is empty, i want the char array to be completely empty.
@suzanaydin "Is there a way?" Sure, like std::fill or a simple loop. "Is there a need?" Probably not since C-style strings are usually defined as a range like [sRecord.m_strUsername[0], first '\0' in the array). What comes behind the first \0 usually does not matter. "Basically, I want the char array to mirror the string in every way" i.e. all unused characters shall be \0? I will add that to my answer.
@suzanaydin there's no such thing as "completely empty" for a char array. It's going to contain 1064 characters no matter what, and each of those characters must equal something.
|
1

You can use strcpy which automatically copies the terminating null character as well, and std::string::c_str:

std::strcpy(sRecord.m_strUsername, strUsername.c_str());

So if strUsername is empty (""), m_strUsername will also be equal to "" after that call.

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.