我有一个简单的程序:
#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)
有谁可以指出这个问题?
Python中静态嵌套块的数量限制为20.也就是说,嵌套19个for循环将很好(虽然过于耗时; O(n^19)是疯狂的),但嵌套20将失败:
SyntaxError: too many statically nested blocks
Run Code Online (Sandbox Code Playgroud)
有这样限制的根本原因是什么?有没有办法增加限额?
如何设置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)
我想避免:
src和include目录src/但是,每个示例都可以拥有自己的构建脚本.
如何将我一直在开发的论坛应用程序转换为Rails引擎,以便它可以嵌入到其他应用程序中?
我应该添加,保留或删除什么?我应该提供一种集成模型的方法吗?如何设置路由和用户配置?如何将其打包成宝石?我应该注意什么?
阅读完文章和文档后,我设法缩小了我的问题范围:
app/models/engine文件夹中吗?config应该保留哪些配置文件?public文件夹怎么样?在Rails 3.1中,样式表和javascripts被移动到app/assets文件夹,这解决了这个问题,但是如何在Rails 3.0中实现相同的效果呢?我正在尝试为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 …
鉴于以下内容:
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)我正在编写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中的引用.
有人可以给我看一个例子来看看标记功能是如何工作的吗?
如何声明一个可以安全指向任何函数的函数指针?
我需要类似void指针的东西,但需要功能.我想过简单地使用它,但根据这个问题的答案,函数指针不能可靠地转换为数据指针,至少在非POSIX系统上.
我搜索并找到了一些可能的例子:
Windows有FARPROC:
int (FAR WINAPI * FARPROC) ()
Run Code Online (Sandbox Code Playgroud)
文档还提到了这一点:
在C中,FARPROC声明指示具有未指定参数列表的回调函数.
听起来不错,但回报值呢?它明确指定为int.
typedef void (*GLfunction)();
extern GLfunction glXGetProcAddressARB(const GLubyte *procName);
Run Code Online (Sandbox Code Playgroud)
参数列表也未指定,但返回类型明确指定为void.
我不知道如何解释这一点.在使用之前,函数指针将使用适当的原型强制转换为函数指针类型.例如:
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标准或导致任何未定义的行为?
我总是习惯于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,而那些没有使用类似于我的解决方案或手动指定文件.
我正在为 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 ×6
ruby ×4
build ×1
build-system ×1
c++ ×1
cmake ×1
file-listing ×1
freestanding ×1
gem ×1
low-level ×1
nested-loops ×1
packaging ×1
pthreads ×1
python ×1
sanitizer ×1
signed ×1