我在这里浏览strlen代码,想知道是否真的需要代码中使用的优化?例如,为什么下面这样的东西不能同样好或更好?
unsigned long strlen(char s[]) {
unsigned long i;
for (i = 0; s[i] != '\0'; i++)
continue;
return i;
}
Run Code Online (Sandbox Code Playgroud)
较简单的代码对编译器进行优化是否更好或更容易?
strlen链接后面页面上的代码如下所示:
Run Code Online (Sandbox Code Playgroud)/* Copyright (C) 1991, 1993, 1997, 2000, 2003 Free Software Foundation, Inc. This file is part of the GNU C Library. Written by Torbjorn Granlund (tege@sics.se), with help from Dan Sahlin (dan@sics.se); commentary by Jim Blandy (jimb@ai.mit.edu). The GNU C Library is free software; you can redistribute it and/or modify it under …
我不确定以下代码是否会导致冗余计算,还是特定于编译器?
for (int i = 0; i < strlen(ss); ++i)
{
// blabla
}
Run Code Online (Sandbox Code Playgroud)
strlen()每次i增加时会计算出来吗?
我的MySQL数据库中有一个描述字段,我在两个不同的页面上访问数据库,一个页面显示整个字段,但另一方面,我只想显示前50个字符.如果说明字段中的字符串少于50个字符,那么它将不会显示...,但如果不是,我将在前50个字符后显示....
示例(完整字符串):
Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters now ...
Run Code Online (Sandbox Code Playgroud)
例2(前50个字符):
Hello, this is the first example, where I am going ...
Run Code Online (Sandbox Code Playgroud) #include "stdio.h"
#include "string.h"
main()
{
char string[] = "october"; // october is 7 letters
strcpy(string, "september"); // september is 9 letters
printf("the size of %s is %d and the length is %d\n\n", string, sizeof(string), strlen(string));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
9月的大小是8,长度是9
我的语法有什么问题或者是什么?
是否可以strlen()在C预处理器中实现?
鉴于:
#define MYSTRING "bob"
Run Code Online (Sandbox Code Playgroud)
是否有一些预处理器宏,X让我说:
#define MYSTRING_LEN X(MYSTRING)
Run Code Online (Sandbox Code Playgroud) 我正在做一些C拼图问题.在大多数情况下,我能够找到正确的答案,但有了这个问题,我就遇到了问题.我通过使用编译器知道正确的答案,但我不知道原因.
看看代码:
char c[] = "abc\012\0x34";
Run Code Online (Sandbox Code Playgroud)
strlen(c)使用标准C编译器会返回什么?
当我期望的是3时,我的编译器返回4.
我的想法是strlen()搜索NULL角色的第一次出现,但不知何故结果是比我预期的更多.
知道为什么吗?
为什么strlen()不检查NULL?
如果我这样做strlen(NULL),就会出现故障.
试图了解它背后的基本原理(如果有的话).
我有一个C程序(一个Apache模块,即程序经常运行),它将write()通过套接字转到0端接的字符串,所以我需要知道它的长度.
字符串#defined为:
#define POLICY "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE cross-domain-policy SYSTEM\n" \
"\"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n" \
"<cross-domain-policy>\n" \
"<allow-access-from domain=\"*\" to-ports=\"8080\"/>\n" \
"</cross-domain-policy>\0"
Run Code Online (Sandbox Code Playgroud)
请问一种方法,比strlen(POLICY)+1在运行时使用更好(从而一次又一次地计算长度)?
一个预处理器指令,它允许POLICY_LENGTH在编译时设置?
我已经搜索了这个警告,并且每个人在他们的代码中都有一些错误,但这是一个非常意外的事情,我无法弄清楚.我们确实期望strlen(x)是一个整数,但这个警告告诉我什么?strlen怎么可能不是int?
In function ‘fn_product’:
line85:3:warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
我在fn_product中的代码 -
char *fn_product (char x[],char y[]){
if (strlen(x)==1) // line85
printf("\nlength of string--%d\n", strlen(x));
/*other code*/
}
Run Code Online (Sandbox Code Playgroud)
不应该strlen(x)为int.为什么它的格式为size_t?
受到关于SQLite3中以下代码的这个问题的启发:
static int strlen30(const char *z){
const char *z2 = z;
while( *z2 ){ z2++; }
return 0x3fffffff & (int)(z2 - z);
}
Run Code Online (Sandbox Code Playgroud)
这附带一条提交消息,说这个函数有助于int溢出.
我对这部分特别感兴趣:
const char *z2 = z;
while( *z2 ){ z2++; }
Run Code Online (Sandbox Code Playgroud)
对我来说,这个循环前进,z2直到z2指向null终止符.然后z2-z产生字符串长度.
为什么不使用strlen()这个部分并重写如下:
return 0x3fffffff & (int)(strlen(z));
Run Code Online (Sandbox Code Playgroud)
为什么使用循环+减法代替strlen()?什么可以循环+减法做什么strlen()不可以?