gpt4 book ai didi

strnlen可以用memchr实现吗?

转载 作者:行者123 更新时间:2023-12-01 10:17:39 24 4
gpt4 key购买 nike

后面的strnlen的实现是否无效?

size_t strnlen(const char *str, size_t maxlen)
{
char *nul = memchr(str, '\0', maxlen);
return nul ? (size_t)(nul - str) : maxlen;
}

我假设 memchr 可能总是查看 maxlen 字节,无论这些字节的内容如何。如果没有 NUL 终止符,strnlen 的契约(Contract)是否只允许它查看所有 maxlen 字节?如果是这样,str 的内存大小可能小于 maxlen 字节,在这种情况下,memchr 可能会尝试读取无效的内存位置。这是正确的吗?

最佳答案

是的,发布的实现是符合的:memchr() 不应该从 str 中读取超过第一次出现的 '\0' .

C17 7.24.5.1 The memchr function

Synopsis

#include <string.h>
void *memchr(const void *s, int c, size_t n);

Description

The memchr function locates the first occurrence of c (converted to an unsigned char) in the initial n characters (each interpreted as unsigned char) of the object pointed to by s. The implementation shall behave as if it reads the characters sequentially and stops as soon as a matching character is found.

Returns

The memchr function returns a pointer to the located character, or a null pointer if the character does not occur in the object.

memchr 可以通过一次测试多个字节的高效技术来实现,可能会读取第一个匹配字节之外的内容,但前提是这不会导致任何可见的副作用。

关于strnlen可以用memchr实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59806803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com