小编Nil*_*esh的帖子

斐波那契搜索

有人请解释我的斐波那契搜索算法.

我已经尝试了很多资源并进行了大量搜索,但算法仍然不清楚.大多数资源都将其描述为与二进制搜索相关联,但我不理解它们.我知道斐波那契搜索算法是二进制搜索的扩展,我很清楚.

我的书也没能解释.

我知道定义为F(n)= F(n-1)+ F(n-2)的斐波纳契数,所以不需要解释.

通过添加我不理解的内容来更新问题@AnthonyLabarre说:

我正在使用的书有奇怪的符号,没有任何解释.在这里发布算法,请帮忙.

if(key == a[mid]) return mid; // understood this, comes from binary search
if(key > a[mid]) {
  if(p == 1) return -1; // What is p? It comes as a function arg
  mid = mid + q; //Now what's this q? Again comes a function arg
  p = p - q; // Commented as p=fib(k-4)
  q = q-p; // q = fib(k-5)
 } else // key < a[mid] {
  if(q == 0) return …
Run Code Online (Sandbox Code Playgroud)

c algorithm search fibonacci

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

在C中用%20替换空格

我正在为我的网站写一个fastcgi应用程序.不要问为什么,留下所有这一部分.

只是帮我解决这个问题 - 我想用%20替换查询字符串中的空格.这是我正在使用的代码,但我没有在输出中看到20,只有%.哪里出了问题?

码:

unsigned int i = 0;

/*
 * Replace spaces with its hex %20
 * It will be converted back to space in the actual processing
 * They make the application segfault in strtok_r()
 */

char *qstr = NULL;
for(i = 0; i <= strlen(qry); i++) {
  void *_tmp;
  if(qry[i] == ' ') {
    _tmp = realloc(qstr, (i + 2) * sizeof(char));
    if(!_tmp) error("realloc() failed while allocting string memory (space)\n");
    qstr = (char *) _tmp;
    qstr[i] …
Run Code Online (Sandbox Code Playgroud)

c http

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

无法调用指向函数的指针返回指针

我遇到了一个奇怪的问题.我有两个文件ac和bc如下:bc:

#include <stdlib.h>

int *foo() {
  int *x;
  x = (int *) malloc(sizeof(int));
  *x = 4;
  return x;
}
Run Code Online (Sandbox Code Playgroud)

我使用gcc编译bc到b.so:$ gcc -o b.so -shared -fpic

AC:

#include <stdio.h>
#include <dlfcn.h>

int main() {
  void *hdl;
  hdl = dlopen("./b.so", RTLD_LAZY);
  int *((*fn)(void));
  int *x;
  x = (*fn)();
  fn = dlsym(hdl, "foo");
  printf("%d", *x);
}
Run Code Online (Sandbox Code Playgroud)

我用gcc编译ac:

$ gcc -fpic -ldl ac

现在当我运行它:

$ ./a.out分段错误

我哪里错了?这在bc中的函数不返回指针时有效.

而且,我尝试使用dlerror()检查错误,但它没有报告.

c pointers function-pointers

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

G ++不接受C++中的内联构造函数

我有一个问题,可能是愚蠢的.

问题是,我无法在类中内联构造函数.

考虑我有一个名为Foo的类.

如果我写这样的Foo的实现:

class Foo {
  int p;
  public:
    Foo() { p = 1; }
};
Run Code Online (Sandbox Code Playgroud)

或者甚至喜欢这样:

class Foo {
  int p;
  public:
    Foo();
};

inline Foo::Foo() {
  p = 1;
}
Run Code Online (Sandbox Code Playgroud)

该程序将无法编译.

我使用标准方法的类:

Foo obj;
Run Code Online (Sandbox Code Playgroud)

现在当我运行g ++ main.cpp foo.cpp时,我得到:

/tmp/ccyVtxvp.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `Foo::Foo()'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

同时,当我使用相同的代码时,在将类编译为共享库(使用工厂函数返回对象指针)之后,它可以正常工作.

有没有猜到为什么会这样?

c++ inline

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

在关闭括号之前C会破坏返回变量吗?

有人告诉我,在C(和C++)中,返回语句中存在的变量在关闭函数的大括号之前被销毁.

前 -

int func() {
    int a = 10;
    return a; // I was told that a is destroyed here
} 
Run Code Online (Sandbox Code Playgroud)

它真的发生了吗?如果是,函数如何将值返回给调用函数?

我的直觉告诉我,变量值被推送到返回值的堆栈,当它返回到调用函数时,通过获取返回值来弹出堆栈顶部.不确定我是否正确.

c

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

标签 统计

c ×4

algorithm ×1

c++ ×1

fibonacci ×1

function-pointers ×1

http ×1

inline ×1

pointers ×1

search ×1