我正在阅读带有glibc源代码的标准库rand()函数实现. stdlib/random_r.c,第359行
int
__random_r (buf, result)
struct random_data *buf;
int32_t *result;
{
int32_t *state;
if (buf == NULL || result == NULL)
goto fail;
state = buf->state;
if (buf->rand_type == TYPE_0)
{
int32_t val = state[0];
val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
state[0] = val;
*result = val;
}
else
{
int32_t *fptr = buf->fptr;
int32_t *rptr = buf->rptr;
int32_t *end_ptr = buf->end_ptr;
int32_t val;
val = *fptr += *rptr;
/* Chucking least random bit. …
Run Code Online (Sandbox Code Playgroud) 我知道这个标题是重复的是这个尾递归吗?,但这是不同的,我无法从前一个问题得到线索.所以我得再问一次.
代码:
int sum_n(int n, int *sum)
{
return n && (*sum += n) && sum_n(n - 1, sum);
}
Run Code Online (Sandbox Code Playgroud)
据说(外语源),尾递归有两个特点:
那么这两个特征是判断尾递归的唯一关键因素吗?&&
返回语句中的逻辑运算符是否会影响尾递归?
最重要的是,上面的代码尾递归?
思想从php-cli
模式下的这个问题开始:
PHP apc/apcu缓存在shmop执行时不保留中间结果,为什么?
在这种情况下,APC/APCu
请不要缓存中间结果.
但是,APC/APCu
请像shmop
在php-fpm
模式下一样缓存中间结果.那么,什么是之间的区别php-cli
和php-fpm
时的问候APC/APCu
?
我对R不是很熟悉,但无论如何我正在为ac库编写一个R包装器.我遇到了这个问题.如何判断输入参数是否为字符串?在细节上,我应该这样写:
dyn.load("hello.so")
do_process <- function(str) {
if(!is.character(str))
stop("not a character or string");
result <- .Call("hello", as.character(str))
return result
}
Run Code Online (Sandbox Code Playgroud)
或这个:
dyn.load("hello.so")
do_process <- function(str) {
if(!is.string(str))
stop("not a character or string");
result <- .Call("hello", as.string(str))
return result
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我在使用PHP将本地结果存储在本地时遇到问题。
用APC
?
apc_store("foo", "bar");
$ret = apc_fetch("foo");
Run Code Online (Sandbox Code Playgroud)
用APCu
?
apcu_store("foo", "bar", 0);
$ret = apcu_fetch("foo");
Run Code Online (Sandbox Code Playgroud)
我将apc_store / apcu_store与apc_store / apcu_store存储在一个php脚本的php_cli下,并与apc_fetch / apcu_fetch一起在另一个php脚本中进行获取,并发现其$ret
为空。
同时,shmop
:
$shmKey = ftok(__FILE__, 't');
$shmId = shmop_open($shmKey, "c", 0644, 1024);
$dataArray = array("foo" => "bar");
shmop_write($shmId, serialize($dataArray), 0);
$retArray = unserialize(shmop_read($shmId, 0, shmop_size($shmId)));
$ret = $retArray['foo'];
Run Code Online (Sandbox Code Playgroud)
在这里,我得到$ret
:"bar"
。
APC/APCu
中间结果是否应该像本地缓存一样shmop
?
我得到这个代码:
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
char *ptr = (char*)malloc(10);
if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing
free(ptr);
}
return;
}
Run Code Online (Sandbox Code Playgroud)
它在Visual C中成功编译,但不在gcc中编译,我得到"错误:'main'必须返回'int'".那么main()函数的返回类型'int'是一个约定(用于编译器定义),还是C的规则?
我正在阅读有关c ++ 11多线程的文档,并且遇到了这个例子std::thread
.
码:
void thread_task(int n) {
...
}
int main(int argc, const char *argv[])
{
std::thread threads[5];
for (int i = 0; i < 5; i++) {
threads[i] = std::thread(thread_task, i + 1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白threads[i] = std::thread(thread_task, i + 1);
.是std::thread
一个静态函数调用,并返回std :: thread对象的引用?听起来不可思议,但似乎是代码所说的.
因为我会这样写:
std::thread *threads[5];
for (int i = 0; i < 5; i++) {
threads[i] = new std::thread(thread_task, i + 1);
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
为什么我的程序不起作用?它应该添加公式1-(1/2)+(1/3)... +(1/999) - (1/1000)=
#include <stdio.h>
#include <math.h>
int main () {
int i, j;
float suma;
suma = 0.f;
for (i=0; i<1000; i++) {
if (i%2==0) {
suma=suma - 1/i;
} else {
suma=suma + 1/i;
}
}
printf("%f", suma);
}
Run Code Online (Sandbox Code Playgroud)