[英]When does printf(“%s”, char*) stop printing?
In my class we are writing our own copy of C's malloc() function. 在我的课上,我们正在编写自己的C的malloc()函数副本。 To test my code (which can currently allocate space fine) I was using:
为了测试我的代码(当前可以分配空间很好)我正在使用:
char* ptr = my_malloc(6*sizeof(char));
memcpy(ptr, "Hello\n", 6*sizeof(char));
printf("%s", ptr);
The output would typically be this: 输出通常是这样的:
Hello
Unprintable character
Some debugging figured that my code wasn't causing this per se, as ptr's memory is as follows: 有些调试认为我的代码本身并没有造成这种情况,因为ptr的内存如下:
[24 bytes of meta info][Number of requested bytes][Padding] [24个字节的元信息] [请求的字节数] [填充]
So I figured that printf was reaching into the padding, which is just garbage. 所以我认为printf正在进入填充,这只是垃圾。 So I ran a test of:
printf("%s", "test\\nd");
所以我运行了一个测试:
printf("%s", "test\\nd");
and got: 得到了:
test
d
Which makes me wonder, when DOES printf("%s", char*) stop printing chars? 这让我想知道,当DOES printf(“%s”,char *)停止打印字符时?
It stops printing when it reaches a null character ( \\0
), because %s
expects the string to be null terminated (ie, it expects the argument to be a C string). 它在到达空字符(
\\0
)时停止打印,因为%s
期望字符串为空终止(即,它期望参数为C字符串)。
The string literal "test\\nd"
is null terminated (all string literals are null terminated). 字符串文字
"test\\nd"
以null结尾(所有字符串文字都以null结尾)。 Your character array ptr
is not, however, because you only copy six characters into the buffer ( Hello\\n
), and you do not copy the seventh character--the null terminator. 但是,您的字符数组
ptr
不是因为您只将六个字符复制到缓冲区( Hello\\n
)中,并且不复制第七个字符 - 空终止符。
James is correct about printf stopping when it gets to the null character, and Uri is correct that you need to allocate a 7-character buffer to hold "hello\\n". 当它到达null字符时,James对于printf停止是正确的,并且Uri是正确的,你需要分配一个7字符的缓冲区来保存“hello \\ n”。
Some of the confusion with terminators would be mitigated if you used the usual C idiom for copying a string: strcpy(ptr, "Hello\\n")
, rather than memcpy
. 如果您使用通常的C语言来复制字符串:
strcpy(ptr, "Hello\\n")
而不是memcpy
,那么与终结符的一些混淆将得到缓解。
Also, by definition, sizeof(char) == 1 in C, so 6*sizeof(char)
is redundant 另外,根据定义,sizeof(char)= = 1 in C,所以
6*sizeof(char)
是多余的
C strings are null terminated (there's a \\0 character at the end), that's how C knows when to stop printing or dealing with the buffer as a string . C字符串以空值终止(末尾有一个\\ 0字符),这就是C知道何时停止打印或将缓冲区作为字符串处理的方式。 It is your responsibility to never put a string in a longer space than what you have allocated.
您有责任永远不要将字符串放在比您分配的更长的空间中。
Note that Hello\\n is not a six character string, it is actually a seven character string. 请注意,Hello \\ n不是六个字符的字符串,实际上是七个字符的字符串。 You use five for the Hello, one for the newline, and one for the null terminator.
你使用五个用于Hello,一个用于换行,一个用于null终止符。
Trying to fit 7 characters into a six character buffer is considered a bug, I am not sure if it is responsible for the problems you are currently having, but it seems like the copying of 6 characters would not copy the null terminator. 尝试将7个字符放入6个字符的缓冲区被认为是一个错误,我不确定它是否对您当前遇到的问题负责,但似乎复制6个字符不会复制空终止符。 So I would actually expect your print to go beyond the Hello and into some actual junk.
所以我真的希望你的印刷品超越Hello,进入一些实际的垃圾。
when it reaches a zero. 当它达到零。 you need 7 chars.
你需要7个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.