how to make a not null-terminated c string?

前端 未结 7 1057
無奈伤痛
無奈伤痛 2020-12-19 07:18

i am wondering :char *cs = .....;what will happen to strlen() and printf(\"%s\",cs) if cs point to memory block which is huge but with no \'\\0\' in it? i write these lines

相关标签:
7条回答
  • 2020-12-19 07:46

    What happens is that strlen keeps going, reading memory values until it eventually gets to a null. it then assumes that is the terminator and returns the length that could be massively large. If you're using strlen in an environment that expects C-strings to be used, you could then copy this huge buffer of data into another one that is just not big enough - causing buffer overrun problems, or at best, you could copy a large amount of garbage data into your buffer.

    Copying a non-null terminated C string into a std:string will do this. If you then decide that you know this string is only 3 characters long and discard the rest, you will still have a massively long std:string that contains the first 3 good characters and then a load of wastage. That's inefficient.

    The moral is, if you're using the CRT functions to operator on C strings, they must be null-terminated. Its no different to any other API, you must follow the rules that API sets down for correct usage.

    Of course, there is no reason you cannot use the CRT functions if you always use the specific-length versions (eg strncpy) but you will have to limit yourself to just those, always, and manually keep track of the correct lengths.

    0 讨论(0)
  • 2020-12-19 07:47

    If it's not null-terminated, then it's not a C string, and you can't use functions like strlen - they will march off the end of the array, causing undefined behaviour. You'll need to keep track of the length some other way.

    You can still print a non-terminated character array with printf, as long as you give the length:

    printf("str is %.3s",s2);
    printf("str is %.*s",s2_length,s2);
    

    or, if you have access to the array itself, not a pointer:

    printf("str is %.*s", (int)(sizeof s2), s2);
    

    You've also tagged the question C++: in that language, you usually want to avoid all this error-prone malarkey and use std::string instead.

    0 讨论(0)
  • 2020-12-19 07:48

    The C standard does not define the term string until the section 7 - Library functions. The definition in C11 7.1.1p1 reads:

    1. A string is a contiguous sequence of characters terminated by and including the first null character.

    (emphasis mine)

    If the definition of string is a sequence of characters terminated by a null character, a sequence of non-null characters not terminated by a null is not a string, period.

    0 讨论(0)
  • 2020-12-19 07:51

    Convention states that a char array with a terminating \0 is a null terminated string. This means that all str*() functions expect to find a null-terminator at the end of the char-array. But that's it, it's convention only.

    By convention also strings should contain printable characters.

    If you create an array like you did char arr[3] = {'a', 'a', 'a'}; you have created a char array. Since it is not terminated by a \0 it is not called a string in C, although its contents can be printed to stdout.

    0 讨论(0)
  • 2020-12-19 07:57

    What you have done is undefined behavior.

    You are trying to write to a memory location that is not yours.

    Change it to

    char s2[] = {'a','a','a','\0'};
    
    0 讨论(0)
  • 2020-12-19 08:01

    Your supposition is correct: your strlen is returning the correct value out of sheer luck, because there happens to be a zero on the stack right after your improperly terminated string. It probably helps that the string is 3 bytes, and the compiler is likely aligning stuff on the stack to 4-byte boundaries.

    You cannot depend on this. C strings need NUL characters (zeroes) at the end to work correctly. C string handling is messy, and error-prone; there are libraries and APIs that help make it less so… but it's still easy to screw up. :)

    In this particular case, your string could be initialized as one of these:

    • A: char s2[4] = { 'a','a','a', 0 }; // good if string MUST be 3 chars long
    • B: char *s2 = "aaa"; // if you don't need to modify the string after creation
    • C: char s2[]="aaa"; // if you DO need to modify the string afterwards

    Also note that declarations B and C are 'safer' in the sense that if someone comes along later and changes the string declaration in a way that alters the length, B and C are still correct automatically, whereas A depends on the programmer remembering to change the array size and keeping the explicit null terminator at the end.

    0 讨论(0)
提交回复
热议问题