- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我编写了以下代码以更好地理解 strnlen 的行为方式:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char bufferOnStack[10]={'a','b','c','d','e','f','g','h','i','j'};
char *bufferOnHeap = (char *) malloc(10);
bufferOnHeap[ 0]='a';
bufferOnHeap[ 1]='b';
bufferOnHeap[ 2]='c';
bufferOnHeap[ 3]='d';
bufferOnHeap[ 4]='e';
bufferOnHeap[ 5]='f';
bufferOnHeap[ 6]='g';
bufferOnHeap[ 7]='h';
bufferOnHeap[ 8]='i';
bufferOnHeap[ 9]='j';
int lengthOnStack = strnlen(bufferOnStack,39);
int lengthOnHeap = strnlen(bufferOnHeap, 39);
printf("lengthOnStack = %d\n",lengthOnStack);
printf("lengthOnHeap = %d\n",lengthOnHeap);
return 0;
}
请注意两个缓冲区中故意缺少空终止。根据文档,长度似乎应该都是 39 岁:
RETURN VALUE The strnlen() function returns strlen(s), if that is less than maxlen, or maxlen if there is no null terminating ('\0') among the first maxlen characters pointed to by s.
这是我的编译行:
$ gcc ./main_08.c -o main
输出:
$ ./main
lengthOnStack = 10
lengthOnHeap = 10
这是怎么回事?谢谢!
最佳答案
首先,strnlen()
C标准没有定义;这是一个 POSIX 标准函数。
话虽如此,请仔细阅读文档
The
strnlen()
function returns the number of bytes in the string pointed to bys
, excluding the terminating null byte ('\0'), but at mostmaxlen
. In doing this,strnlen()
looks only at the firstmaxlen
bytes ats
and never beyonds+maxlen
.
这意味着,在调用该函数时,您需要确保对于您为 maxlen
提供的值,数组索引对 [maxlen -1]
对于提供的字符串,即 string 中至少有 maxlen
个元素。
否则,在访问 string 时,您将冒险进入未分配给您的内存位置(数组越界访问),从而调用 undefined behaviour .
请记住,此函数用于计算数组的长度,上限为一个值 (maxlen
)。这意味着,提供的数组至少等于或大于边界,而不是相反。
[脚注]:
根据定义,字符串 以 null 结尾。
引用 C11
,章节 §7.1.1,术语定义
A string is a contiguous sequence of characters terminated by and including the first null character. [...]
关于c - 当 strnlen() 的最大长度大于实际缓冲区大小时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719026/
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
在我当前的项目中,我根据 C11 标准进行编码(使用 gcc -std=c11 构建)并且需要类似 strnlen 的东西(strlen 的“安全”版本,它返回一个以 0 结尾的字符串的长度,但只能达
我编写了以下代码以更好地理解 strnlen 的行为方式: #include #include #include int main(int argc, char **argv) { ch
我很困惑 strnlen 如何在 C++ 中使用非 null 终止的字符串,因为我不确定它如何计算大小。 strlen 的实现很简单: size_t strlen(char *s) { siz
函数 strdup() 和 strndup() 终于进入了即将到来的 C23 标准: 7.24.6.4 The strdup function Synopsis #include char *str
我正在尝试在 LPCXpresso 上为 LPC1769 创建一个项目。我有一个 C 文件调用 #include int main() { //some stuff strnlen(
这是我的情况,我使用 TDM-GCC 4.8.1 从源代码构建了 wxWidgets 3.0。然后我们使用 Haskell 的 cabal系统构建依赖于 wx(例如 wxHaskell)的某些包,ca
我的程序是: #include #include using namespace std; int main(int argc,char **argv) { const static si
我有在 .mm 文件中使用 strnlen 的代码 使用 Xcode 4.2 时编译器可以正常构建。然而 Xcode 3.2.5 找不到 strnlen()... 我已经尝试过针对 strlen 发布
我可能要疯了,但我认为我从未在 C++ 中看到过这种情况(尽管我的引用代码是在 C 中)。为什么这里代码的返回值上有一个static,有什么影响?我认为我从未见过类范围之外的静态函数(但显然 C 没有
我正在构建最新 dcraw.c 的跨平台 OS X 版本我在 OS X 10.6.8 上执行此操作以获得 PPC 兼容性。现在我的问题是 strnlen 似乎在最新版本的程序中使用,它在 10.6.8
我是一名优秀的程序员,十分优秀!