我开始学习标准ML,现在我尝试使用新泽西标准ML编译器.
现在我可以使用交互式循环,但是如何将源文件编译为独立的可执行文件?
例如,在C中,人们可以写
$ gcc hello_world.c -o helloworld
然后运行helloworld二进制文件.
我阅读了SML NJ编译管理器的文档,但它没有任何明确的例子.
另外,是否有另一个SML编译器(允许独立二进制创建)可用?
plain C有很好的功能 - void类型指针,可以用作指向任何数据类型的指针.
但是,假设我有以下结构:
struct token {
int type;
void *value;
};
Run Code Online (Sandbox Code Playgroud)
其中value字段可以指向char数组,或指向int或其他内容.
所以在分配这个结构的新实例时,我需要:
1)为这个结构分配内存;
2)为值分配内存并将其分配给值字段.
我的问题是 - 有没有办法声明" 类型为void的数组 ",它可以被转换为任何其他类型,如void指针?
我想要的只是使用"灵活的成员阵列"(在C99标准的6.7.2.1中描述),能够投射到任何类型.
像这样的东西:
struct token {
int type;
void value[];
};
struct token *p = malloc(sizeof(struct token) + value_size);
memcpy(p->value, val, value_size);
...
char *ptr = token->value;
Run Code Online (Sandbox Code Playgroud)
我假设将token-> value声明为char或int数组,并且稍后转换为所需类型将执行此工作,但对于稍后将阅读此代码的人来说可能会非常混乱.
假设有一些使用全局变量的不可重入函数:
int i;
void foo(void){
/* modify i */
}
Run Code Online (Sandbox Code Playgroud)
然后,我想在多线程代码中使用此函数,因此我可以这样更改代码:
void foo(int i){
/* modify i */
}
Run Code Online (Sandbox Code Playgroud)
或者,通过使用gcc __thread说明符,更简单:
__thread int i;
void foo(void){
/* modify i */
}
Run Code Online (Sandbox Code Playgroud)
最后的优点是我不需要更改另一个调用foo()的代码.
我的问题是,线程本地存储的开销是多少?TLS有一些不明显的问题吗?
如果我将通过单独的指针修改TLS`ed变量,是否有一些开销,如下所示:
__thread int i;
void foo(void){
int *p = &i;
/* modify i using p pointer */
}
Run Code Online (Sandbox Code Playgroud)
谢谢.