strlen

strlen performance implementation

一笑奈何 提交于 2019-11-27 18:12:41
问题 This is a multipurpose question: How does this compare to the glibc strlen implementation? Is there a better way to to this in general and for autovectorization. Random filler crap because stackoverflow somehow knows better than me about code to summary ratio #include <stdint.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdbool.h> #include <stdlib.h> /* Todo: Document */ #define WORD_ONES_LOW ((size_t)-1 / UCHAR_MAX) #define WORD_ONES_HIGH (((size_t)-1 / UCHAR_MAX)

【译】PHP 内核 — 字符串管理

女生的网名这么多〃 提交于 2019-11-27 16:11:56
【译】PHP 内核 — 字符串管理 (Strings management: zend_string 译文) 原文地址: http://www.phpinternalsbook.com/php7/internal_types/strings/zend_strings.html 原文仓库: https://github.com/phpinternalsbook/PHP-Internals-Book 原文作者: phpinternalsbook 译文出自: https://github.com/suhanyujie 本文永久链接: 译者: suhanyujie 翻译不当之处,还请指出,谢谢! 字符串管理:zend_string 任何程序都需要管理字符串。在这里,我们将详细介绍一个适合 PHP 需要的定制解决方案: zend_string 。每次 PHP 需要处理字符串时,都会使用 zend_string 结构体。这个结构体是 C 语言的 char * 类型包装后的结果。 它增加了内存管理功能,这样的话,同一个字符串可以在多个地方共享,而不需要复制它。另外,有些字符串是 “interned” 的,它们被持久化的分配内存并由内存管理器进行特殊管理的,这样可以实现在多个请求之间进行复用。那些字符串在后续会被 Zend 内存管理器 持久化分配。 结构体和访问宏 这里简单地展示 zend

How to find the length of argv[] in C

坚强是说给别人听的谎言 提交于 2019-11-27 15:57:10
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]){ int fir; //badly named loop variable char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array for( fir = 1; fir< strlen(argv); fir++){ //removing the first element of argv strcat(input, argv[fir]); // appending to input } } The error I'm getting is for line 7. It says "passing argument 1 of 'strlen' from incompatible pointer type". I get the same error for the strcat function. It

strlen - the length of the string is sometimes increased by 1

狂风中的少年 提交于 2019-11-27 14:57:05
I'm doing some C puzzle questions. In most cases, I am able to find the right answer, but with that one I am having problems. I know the right answer by using the compiler, but I don't know the reason. Have a look at the code: char c[] = "abc\012\0x34"; What would strlen(c) return, using a Standard C compiler? My compiler returns 4 when what I expected was 3. What I thought is strlen() would search for the first occurrence of the NULL character but somehow the result is one more than I expected. Any idea why? Let's write char c[] = "abc\012\0x34"; with single characters: char c[] = { 'a', 'b',

SQLite学习手册(实例代码<二>)

寵の児 提交于 2019-11-27 08:54:29
三、高效的批量数据插入: 在给出操作步骤之前先简单说明一下批量插入的概念,以帮助大家阅读其后的示例代码。事实上,批量插入并不是什么新的概念,在其它关系型数据库的C接口API中都提供了一定的支持,只是接口的实现方式不同而已。纵观众多流行的数据库接口,如OCI(Oracle API)、MySQL API和PostgreSQL API等,OCI提供的编程接口最为方便,实现方式也最为高效。SQLite作为一种简单灵活的嵌入式数据库也同样提供了该功能,但是实现方式并不像其他数据库那样方便明显,它只是通过一种隐含的技巧来达到批量插入的目的,其逻辑如下: 1). 开始一个事物,以保证后面的数据操作语句均在该事物内完成。在SQLite中,如果没有手工开启一个事物,其所有的DML语句都是在自动提交模式下工作的,既每次操作后数据均被自动提交并写入磁盘文件。然而在非自动提交模式下,只有当其所在的事物被手工COMMIT之后才会将修改的数据写入到磁盘中,之前修改的数据都是仅仅驻留在内存中。显而易见,这样的批量写入方式在效率上势必会远远优于多迭代式的单次写入操作。 2). 基于变量绑定的方式准备待插入的数据,这样可以节省大量的sqlite3_prepare_v2函数调用次数,从而节省了多次将同一SQL语句编译成SQLite内部识别的字节码所用的时间。事实上,SQLite的官方文档中已经明确指出

Sizeof vs Strlen

我是研究僧i 提交于 2019-11-27 07:04:33
#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; } Output: the size of september is 8 and the length is 9 Is there something wrong with my syntax or what? Sean sizeof and strlen() do different things. In this case, your declaration char string[] = "october"; is the same as char string[8] = "october"; so the compiler can tell that the size of string is 8. It does this at compilation

Quick strlen question

做~自己de王妃 提交于 2019-11-27 06:57:08
问题 I've come to bother you all with another probably really simple C question. Using the following code: int get_len(char *string){ printf("len: %lu\n", strlen(string)); return 0; } int main(){ char *x = "test"; char y[4] = {'t','e','s','t'}; get_len(x); // len: 4 get_len(y); // len: 6 return 0; } 2 questions. Why are they different and why is y 6? Thanks guys. EDIT: Sorry, I know what would fix it, I kind of just wanted to understand what was going on. So does strlen just keep forwarding the

Why does glibc's strlen need to be so complicated to run quickly?

谁都会走 提交于 2019-11-27 04:58:55
问题 I was looking through the strlen code here and I was wondering if the optimizations used in the code are really needed? For example, why wouldn't something like the following work equally good or better? unsigned long strlen(char s[]) { unsigned long i; for (i = 0; s[i] != '\0'; i++) continue; return i; } Isn't simpler code better and/or easier for the compiler to optimize? The code of strlen on the page behind the link looks like this: /* Copyright (C) 1991, 1993, 1997, 2000, 2003 Free

Tinyhttp源码分析

半世苍凉 提交于 2019-11-27 04:11:00
简介 Tinyhttp是一个轻量型Http Server,使用C语言开发,全部代码只500多行,还包括一个简单Client。 Tinyhttp程序的逻辑为:一个无线循环,一个请求,创建一个线程,之后线程函数处理每个请求,然后解析HTTP请求,做一些判断,之后判断文件是否可执行,不可执行,打开文件,输出给客户端(浏览器),可执行就创建管道,父子进程进行通信。父子进程通信,用到了dup2和execl函数。 模型图 源码剖析 1 #include <stdio.h> 2 #include <sys/socket.h> 3 #include <sys/types.h> 4 #include <netinet/in.h> 5 #include <arpa/inet.h> 6 #include <unistd.h> 7 #include <ctype.h> 8 #include <strings.h> 9 #include <string.h> 10 #include <sys/stat.h> 11 #include <pthread.h> 12 #include <sys/wait.h> 13 #include <stdlib.h> 14 15 #define ISspace(x) isspace((int)(x)) 16 17 #define SERVER_STRING "Server:

char数组与char指针

旧时模样 提交于 2019-11-27 01:18:53
1、以字符串形式出现的,编译器会在结尾自动添加\0,思考,为什么?   存在的C语言方法,如strlen(s),计算字符串的长度,其中s指针。strlen要计算字符串长度,必须知道哪里是结尾,因此使用\0表示结尾。只有字符数组才有\0的概念,其它类型(int)的数组没有这个概念。因为其他类型的数组或者指针,没有strlen这种方法。   那么问题来了,int数组如何计算长度呢?如int a1 = {3,7,9,};   使用sizeof(a1)/sizeof(int)。 2、数组可以在栈上分配,也可以在堆上分配,但必须指定大小。   char a1[100]; //在栈上分配   char* pa = new char[100];// 在堆上分配,返回首元素的地址 3、char a1[] = "abc"; 相当于在栈顶分配4个字节,分别放上a,b,c,\0,等价于char a1 ={'a','b','c','\0'}; 4、char* pa = "abc"; 分析一下就知道,pa是char指针,"abc"是一个文本字符串,显然类型不吻合,需要适配。可认为编译器做了下面的事情:在常量区分配4个字节,分别放上a,b,c,\0,然后把a的地址返回给pa。   注意:文本字符串放在常量区,是不可修改的,试图修改,运行异常。那么在思考一下,既然右边是const,而pa并没有限定为const