我在汇编中实现了strlen的自己的实现,但没有返回正确的值。它返回字符串长度+4。因此。我不明白为什么..我希望任何你...
大会来源:
section .text
[GLOBAL stringlen:] ; C function
stringlen:
push ebp
mov ebp, esp ; setup the stack frame
mov ecx, [ebp+8]
xor eax, eax ; loop counter
startLoop:
xor edx, edx
mov edx, [ecx+eax]
inc eax
cmp edx, 0x0 ; null byte
jne startLoop
end:
pop ebp
ret
Run Code Online (Sandbox Code Playgroud)
和主要例程:
#include <stdio.h>
extern int stringlen(char *);
int main(void)
{
printf("%d", stringlen("h"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我想知道在尝试编写strlen函数以\0并行查找序列时是否有任何优点.如果是这样,这样的功能应该考虑什么?谢谢.
让我们考虑一下这个简单的测试程序:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buf[256];
int i;
strcpy(buf,"Hello world!");
i = strlen(buf);
printf("Length of string is %d.\n",i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在使用英特尔c ++编译器进行编译并打开优化(O3)时,我从valgrind得到以下错误:
==8727== Conditional jump or move depends on uninitialised value(s)
==8727== at 0x4009EF: main (strtest.cpp:11)
==8727== Use of uninitialised value of size 8
==8727== at 0x4FC61ED: _itoa_word (in /lib64/libc-2.4.so)
==8727== by 0x4FC9317: vfprintf (in /lib64/libc-2.4.so)
==8727== by 0x4FD02A9: printf (in /lib64/libc-2.4.so)
==8727== by 0x400A09: main (strtest.cpp:13)
==8727== Conditional jump or move …Run Code Online (Sandbox Code Playgroud) 我正在查看glibc 的 strlen的来源。他们用来magic bits求字符串的长度。有人可以解释一下它是如何工作的吗?谢谢
使用strlen实际上是通过迭代字符串来计算字符串中的字节数,还是简单地从索引返回已经计算的字符串长度的值?
我的问题的原因是因为我可以选择为速度敏感的脚本存储非常长字符串的预先计算值,或者我可以使用该strlen函数并节省自己的编码时间.
但我真的想知道我是如何strlen工作的,因为我倾向于依赖它,也许这不是一个好主意?
UPDATE
请参阅下面的基准.
我很好奇strlen如何计算C中多个字节的unicode字符.
是否计算每个字节或字符(因为它们可以由几个字节组成)直到第一个'\ 0'?
在VS2013中处理这段小代码,但由于某种原因它没有print.it似乎-1> strlen(str)
任何人都知道我做错了什么
char *str="abcd";
if(-1<strlen(str))
printf("The size of the string is %d", strlen(str));
return 0;
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
int main(int argc, char** argv)
{
char* p = new char[11];
strcpy(p, "1234567890");
cout << strlen(p) << endl;
delete[] p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它分配11个字节,然后复制一个10字节的字符串加上一个nul终结符.这对我来说似乎很正确.
但如果我用Valgrind运行它,我得到这个:
bash-4.3$ valgrind ./a.out
...
==44295== Command: ./a.out
==44295==
==44295== Invalid read of size 8
==44295== at 0x3E6073382F: __strlen_sse42 (in /lib64/libc-2.12.so)
==44295== by 0x4008A9: main (in /bb/mbig_new2/mbig3978/bbgithub/tsacqdata/tsacqdata/unit_test/Cache/a.out)
==44295== Address 0x4c2d048 is 8 bytes inside a block of size 11 alloc'd
==44295== at 0x4A06FE8: operator new[](unsigned long) (vg_replace_malloc.c:363)
==44295== by 0x40087E: main (in /bb/mbig_new2/mbig3978/bbgithub/tsacqdata/tsacqdata/unit_test/Cache/a.out)
...
为什么Valgrind不喜欢那个 …
我有一些数据需要清理成固定长度的格式。我正在使用 PHP 获取数据,将其隐藏,然后放回原处,但它没有按计划工作。数据中间的每一块都有一个特定的点,该点应该有空格以将长度增加到适当的字符数。我用来执行此操作的代码是:
while ($row = mysql_fetch_array($databasetable)) {
$key = $row['KEY'];
$strlength = strlen($key);
while ($strlength < 33) {
$array = explode(' TA',$key);
$key = $array[0] . ' TA' . $array[1];
$strlength++;
}
}
Run Code Online (Sandbox Code Playgroud)
它需要一个'TA'并在它之前添加两个空格,冲洗并重复直到总长度为33,但是当我输出值时,它只返回一个空格。有趣的是,即使它显示一个空格,它也会返回 33 的 strlen,即使它不显示 33 个字符。
任何帮助解决这个问题将不胜感激。
你好我每个人都写过代码
char sentence[100];
scanf("%s" ,sentence);
char * ptrs = sentence ;
printf("%d", strlen(ptrs));
Run Code Online (Sandbox Code Playgroud)
假设我进入
john is a boy
Run Code Online (Sandbox Code Playgroud)
该strlen()功能是给我值4停止的空间是我应该做的感谢后,计数