小编lul*_*yon的帖子

glibc rand函数实现

我正在阅读带有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)

c random glibc

4
推荐指数
2
解决办法
7739
查看次数

这个尾递归即使是逻辑 - 并且存在吗?

我知道这个标题是重复的是这个尾递归吗?,但这是不同的,我无法从前一个问题得到线索.所以我得再问一次.

代码:

int sum_n(int n, int *sum)
{
    return n && (*sum += n) && sum_n(n - 1, sum);
}
Run Code Online (Sandbox Code Playgroud)

据说(外语源),尾递归有两个特点:

  1. 该功能是自称的
  2. 只需要恒定的堆栈空间

那么这两个特征是判断尾递归的唯一关键因素吗?&&返回语句中的逻辑运算符是否会影响尾递归?

最重要的是,上面的代码尾递归?

c recursion tail-recursion

3
推荐指数
1
解决办法
309
查看次数

关于APC/APCu的php-cli和php-fpm模式之间的区别

思想从php-cli模式下的这个问题开始:

PHP apc/apcu缓存在shmop执行时不保留中间结果,为什么?

在这种情况下,APC/APCu请不要缓存中间结果.

但是,APC/APCu请像shmopphp-fpm模式下一样缓存中间结果.那么,什么是之间的区别php-cliphp-fpm时的问候APC/APCu

php apc

3
推荐指数
1
解决办法
2254
查看次数

R中字符串和字符有什么区别?

我对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)

谢谢.

string r

2
推荐指数
2
解决办法
1万
查看次数

PHP apc / apcu缓存不能保持中间结果,而shmop可以,为什么?

我在使用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

php caching apc apcu

2
推荐指数
1
解决办法
361
查看次数

main()函数的返回类型

我得到这个代码:

#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 program-entry-point

1
推荐指数
1
解决办法
1万
查看次数

无法理解std :: thread的用法

我正在阅读有关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)

谢谢.

c++ c++11 stdthread

1
推荐指数
1
解决办法
212
查看次数

C - 数字之和

为什么我的程序不起作用?它应该添加公式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)

c sum

0
推荐指数
2
解决办法
877
查看次数

标签 统计

c ×4

apc ×2

php ×2

apcu ×1

c++ ×1

c++11 ×1

caching ×1

glibc ×1

program-entry-point ×1

r ×1

random ×1

recursion ×1

stdthread ×1

string ×1

sum ×1

tail-recursion ×1