小编Mat*_*ira的帖子

为什么0 <-0x80000000?

我有一个简单的程序:

#include <stdio.h>

#define INT32_MIN        (-0x80000000)

int main(void) 
{
    long long bal = 0;

    if(bal < INT32_MIN )
    {
        printf("Failed!!!");
    }
    else
    {
        printf("Success!!!");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

条件if(bal < INT32_MIN )总是如此.这怎么可能?

如果我将宏更改为:

#define INT32_MIN        (-2147483648L)
Run Code Online (Sandbox Code Playgroud)

有谁可以指出这个问题?

c signed numeric-conversion numeric-limits

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

为什么Python对可以嵌套的静态块的数量有限制?

Python中静态嵌套块的数量限制为20.也就是说,嵌套19个for循环将很好(虽然过于耗时; O(n^19)是疯狂的),但嵌套20将失败:

SyntaxError: too many statically nested blocks
Run Code Online (Sandbox Code Playgroud)

有这样限制的根本原因是什么?有没有办法增加限额?

python language-implementation nested-loops

59
推荐指数
2
解决办法
5649
查看次数

让CMake递归扫描文件夹?

如何设置CMake以递归方式扫描给定目录并确定源文件列表?

我的项目是一个共享库.我有一个与此类似的文件夹结构:

/
  src/              # Source files in an arbitrary tree
  include/          # Headers, tree mirrors that of the src/ folder
  examples/         # Executable code examples that link against the library
  CMakeLists.txt
Run Code Online (Sandbox Code Playgroud)

我想避免:

  • 用无尽的文件污染srcinclude目录src/
  • 每次我更改文件夹结构时都必须更改和修改脚本

但是,每个示例都可以拥有自己的构建脚本.

build-automation build-process build cmake build-system

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

如何将现有的Rails 3应用程序转换为引擎?

如何将我一直在开发的论坛应用程序转换为Rails引擎,以便它可以嵌入到其他应用程序中?

我应该添加,保留或删除什么?我应该提供一种集成模型的方法吗?如何设置路由和用户配置?如何将其打包成宝石?我应该注意什么?


阅读完文章和文档后,我设法缩小了我的问题范围:

  • 我应该命名模型吗?也就是说,我应该将它们保存在我的引擎模块和app/models/engine文件夹中吗?
  • config应该保留哪些配置文件?
  • public文件夹怎么样?在Rails 3.1中,样式表和javascripts被移动到app/assets文件夹,这解决了这个问题,但是如何在Rails 3.0中实现相同的效果呢?

ruby ruby-on-rails ruby-on-rails-plugins ruby-on-rails-3

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

在C中扩展ruby - 如何指定默认参数值来运行?

我正在尝试为ruby写一个C扩展,它将生成一个类.我正在研究如何为类定义一些默认参数.例如,如果我在ruby中有这个类的decleration:

class MyClass
  def initialize(name, age=10)
    @name = name
    @age  = age
  end
end
Run Code Online (Sandbox Code Playgroud)

您可以使用它进行初始化mc = MyClass.new("blah"),并在内部设置age参数.我怎么用C做这个?到目前为止,我得到了这个,但这迫使进入另一个论点:

require "ruby.h"

static VALUE my_init(VALUE self, VALUE name, VALUE age)
{
    rb_iv_set(self, "@name", name);
    rb_iv_set(self, "@age", age);

    return self;
}

VALUE cMyClass;

void Init_MyClass() 
{
    // create a ruby class instance
    cMyClass = rb_define_class("MyClass", rb_cObject);

    // connect the instance methods to the object
    rb_define_method(cMyClass, "initialize", my_init, 2);
}
Run Code Online (Sandbox Code Playgroud)

我考虑过检查age反对Qnil或使用的价值if ( TYPE(age) == T_UNDEF ),但我只是从那里得到段错误.阅读README.EXT …

c ruby ruby-c-extension

16
推荐指数
2
解决办法
3429
查看次数

pthread_create()如何工作?

鉴于以下内容:

pthread_t thread;
pthread_create(&thread, NULL, function, NULL);
Run Code Online (Sandbox Code Playgroud)
  • 究竟pthread_create做了thread什么?

  • thread它加入主线程并终止后会发生什么?

  • 如果在thread加入后,您会这样做:

    pthread_create(&thread, NULL, another_function, NULL);
    
    Run Code Online (Sandbox Code Playgroud)

c c++ pthreads

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

Data_wrap_struct和标记功能

我正在编写Ruby扩展并使用该函数Data_wrap_struct.

为了参与Ruby的标记和清除垃圾收集过程,我需要定义一个例程来释放我的结构,以及一个例程来标记从我的结构到其他结构的任何引用.我通过经典free函数释放内存,但我不知道如何使用标记功能.

我的结构听起来像这样

typedef struct
{
  int x;
  int y;
} A;

typedef struct
{
  A collection[10];
  int current;
} B;
Run Code Online (Sandbox Code Playgroud)

我认为我需要一个标记函数来标记collection结构B中的引用.

有人可以给我看一个例子来看看标记功能是如何工作的吗?

c ruby ruby-c-extension

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

指向任何功能?

如何声明一个可以安全指向任何函数的函数指针?

我需要类似void指针的东西,但需要功能.我想过简单地使用它,但根据这个问题的答案,函数指针不能可靠地转换为数据指针,至少在非POSIX系统上.

我搜索并找到了一些可能的例子:

我不知道如何解释这一点.在使用之前,函数指针将使用适当的原型强制转换为函数指针类型.例如:

typedef void (*function_pointer_t)();
extern function_pointer_t get_function(const char * name);

/* Somewhere else... */

typedef double (*cosine_function_t)(double);
cosine_function_t cosine = (cosine_function_t) get_function("cos");
Run Code Online (Sandbox Code Playgroud)

这段代码安全吗?它是否违反任何C标准或导致任何未定义的行为?

c function-pointers

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

确定gem的规范文件列表

我总是习惯于git确定哪些文件应该进入gem包:

gem.files = `git ls-files`.split "\n"
Run Code Online (Sandbox Code Playgroud)

不幸的是,这种方法最近被证明是不合适的.我需要一个独立的纯Ruby解决方案.

我的第一个想法是简单地整理整个目录,但仅此一项可能包括不需要的文件.所以,在研究了这个问题之后,我想出了这个:

# example.gemspec

directory = File.dirname File.expand_path __FILE__
dotfiles = %w(.gitignore .rvmrc)
ignore_file = '.gitignore'
file_list = []

Dir.chdir directory do
  ignored = File.readlines(ignore_file).map(&:chomp).reject { |glob| glob =~ /\A(#|\s*\z)/ }
  file_list.replace Dir['**/**'] + dotfiles
  file_list.delete_if do |file|
    File.directory?(file) or ignored.any? { |glob| File.fnmatch? glob, file }
  end
end

# Later...

gem.files = file_list
Run Code Online (Sandbox Code Playgroud)

这似乎有点复杂gemspec.它也不完全支持gitignore模式格式.它目前似乎有效,但我宁愿以后再遇到问题.

是否有一种更简单但更健壮的方法来计算gem的文件列表?大多数宝石显然都在使用git ls-files,而那些没有使用类似于我的解决方案或手动指定文件.

ruby gem packaging file-listing

7
推荐指数
2
解决办法
1549
查看次数

清理静态独立 nolibc 程序?

我正在为 Linux 开发一个静态独立的 nolibc/nostdlib 程序,并希望使用 C 编译器的内存、地址和未定义行为清理程序来改进我的代码。

当我尝试时,我无法让它工作:

clang -static -ffreestanding -nostdlib -fno-omit-frame-pointer -fsanitize=undefined -g -o program program.c
Run Code Online (Sandbox Code Playgroud)

这会导致编译器发出调用诸如__ubsan_handle_type_mismatch_v1@plt. 它编译和链接成功,但程序在运行时在这些引用附近出现段错误。更具体地说,在我的内存分配器中:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) up
(gdb) disas
Dump of assembler code for function lone_reallocate:
...
   0x00000000002116d0 <+160>:   bl      0x206470 <__ubsan_handle_pointer_overflow@plt>
=> 0x00000000002116d4 <+164>:   b       0x2116d8 <lone_reallocate+168>
   0x00000000002116d8 <+168>:   ldur    x8, [x29, #-72]
...
Run Code Online (Sandbox Code Playgroud)

我认为这些函数由于缺乏 libc 支持而丢失。当我尝试使用该-static-libsan选项时,出现许多未定义的符号错误:

error: undefined symbol: __aarch64_cas8_acq_rel
error: undefined symbol: pthread_self
error: undefined symbol: dl_iterate_phdr
error: …
Run Code Online (Sandbox Code Playgroud)

c low-level sanitizer freestanding address-sanitizer

7
推荐指数
0
解决办法
202
查看次数