1

When I run the example code, the wordLength is 7 (hence the output 7). But my char array gets some really weird characters in the end of it.

wordLength = word.length();

cout << wordLength;

char * wordchar = new char[wordLength]; //new char[7]; ??

for (int i = 0; i < word.length(); i++) //0-6 = 7
{
    wordchar[i] = 'a';
}

cout << wordchar;

The output: 7 aaaaaaa²²²²¦¦¦¦¦ÂD╩2¦♀

Desired output is: aaaaaaa... What is the garbage behind it?? And how did it end up there?

user2141625
  • 271
  • 1
  • 3
  • 10

4 Answers4

5

You should add \0 at the end of wordchar.

char * wordchar = new char[wordLength +1];
//add chars as you have done
wordchar[wordLength] = `\0`

The reason is that C-strings are null terminated.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • Is it because it's a dynamic char I need to do that myself? – user2141625 Sep 29 '13 at 16:22
  • No, as other posts indicate, it is because C-string are null terminated, which means that you need to add `\0` at the end to serve for this purpose. – taocp Sep 29 '13 at 16:23
3

C strings are terminated with a '\0' character that marks their end (in contrast, C++ std::string just stores the length separately).

In copying the characters to wordchar you didn't terminate the string, thus, when operator<< outputs wordchar, it goes on until it finds the first \0 character that happens to be after the memory location pointed to by wordchar, and in the process it prints all the garbage values that happen to be in memory in between.

To fix the problem, you should:

  1. make the allocated string 1 char longer;
  2. add the \0 character at the end.

Still, in C++ you'll normally just want to use std::string.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

Use: -

char * wordchar = new char[wordLength+1]; // 1 extra for null character

before for loop and

wordchar[i] ='\0'

after for loop , C strings are null terminated.

Without this it keeps on printing, till it finds the first null character,printing all the garbage values.

P0W
  • 46,614
  • 9
  • 72
  • 119
1

You avoid the trailing zero, that's the cause.

In C and C++ the way the whole eco-system treats string length is that it assumes a trailing zero ('\0' or simply 0 numerically). This is different then for example pascal strings, where the memory representation starts with the number which tells how many of the next characters comprise the particular string.

So if you have a certain string content what you want to store, you have to allocate one additional byte for the trailing zero. If you manipulate memory content, you'll always have to keep in mind the trailing zero and preserve it. Otherwise strstr and other string manipulation functions can mutate memory content when running off the track and keep on working on the following memory section. Without trailing zero strlen will also give a false result, it also counts until it encounters the first zero.

You are not the only one making this mistake, it often gets important roles in security vulnerabilities and their exploits. The exploit takes advantage of the side effect that function go off trail and manipulate other things then what was originally intended. This is a very important and dangerous part of C.

In C++ (as you tagged your question) you better use STL's std::string, and STL methods instead of C style manipulations.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121