3

I have a function

ClassA::FuncA(const char *filePath)

and want to copy this const char string* to a char*!

My solution:

char *argv[2];
int length = strlen(filePath);
argv[1] = new char(length +1);
strncpy(argv[1], filePath, length); 

after this I have in argv[1] the desired chars but also some other undefined chars!

filePath:

"C:\Users\userA\Parameter.xmlþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþKŸQyá•"

Whats wrong here? The length with strlen is OK!

razlebe
  • 7,134
  • 6
  • 42
  • 57
leon22
  • 5,280
  • 19
  • 62
  • 100

4 Answers4

16

Like so:

argv[1] = new char[length +1](); // () to value-initialize the array

Your version:

argv[1] = new char(length +1);

only allocates a single char and value-initializes it to length+1.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • `argv[1] = new char[length+1]; strncpy(argv[1], filePath, length+1);` could be faster for large lengths?! Well in most cases it wont matter i guess. – smerlin Jun 13 '12 at 08:01
  • i pressed enter for a newline ...well does not work in comments, so my incomplete comment was visible for a second. you are to fast! – smerlin Jun 13 '12 at 08:03
  • His use of `strncpy` is also wrong (but the syntax error in the `new` was well spotted). – James Kanze Jun 13 '12 at 08:23
1

You have two problems in your code:

  • You need to add 1 to length after copying in order to copy null character (as strlen returns only number of chars without null character; see more here). So change code to:

    strncpy(argv[1], filePath, length + 1);

  • You need fix how your array is being initialized as you are initializing only one character (and we assume you want full string to be copied). So:

    argv[1] = new char[length + 1]();


Notes:

  • Please when you post also post the code that was used to print out data as problems such as these in a lot of cases depend on what you call to print out data.
  • And at the end you might consider using just an array of fixed size that is initialized to maximum path. For max path size in windows checkout following post
Community
  • 1
  • 1
Bo.
  • 2,547
  • 3
  • 24
  • 36
1

The problem is that you're using strncpy, rather than strcpy. And the way you're using it, it doesn't copy the terminating \0.

In practice, because strncpy may leave a string without a \0 terminator, it's best to avoid it. In your case, strcpy alone is fine, since you've just allocated a sufficiently large buffer. In the more general case, you may have to use strlen, to ensure that the string you have fits in the target buffer (without ever forgetting to add 1 to the results, for the \0).

If the situation occurs a lot, you might want to write your own version of strncpy, which works (i.e. guarantees a terminating \0, and doesn't copy or write more characters than necessary). Something like:

void
stringCopy( char* dest, int maxLength, char const* source )
{
    assert( maxLength > 0 );
    char* end = dest + maxLength - 1;
    while ( dest != end && *source != '\0' ) {
        *dest = *source;
        ++ dest;
        ++ source;
    }
    *dest = '\0';
}

(This function actually exists, under the name strcpy_s in C 2011, but it isn't widely implemented; Microsoft has it, but I've not seen it elsewhere.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

strncpy() copies not more than length characters. In doing so, terminating \0 was not copied.

mouviciel
  • 66,855
  • 13
  • 106
  • 140