sizeof在给出三元表达时,我很难理解行为.
#define STRING "a string"
int main(int argc, char** argv)
{
int a = sizeof(argc > 1 ? STRING : "");
int b = sizeof(STRING);
int c = sizeof("");
printf("%d\n" "%d\n" "%d\n", a, b, c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在此示例中(使用gcc 4.4.3和4.7.2进行测试,编译时-std=c99),b为9(8个字符+隐式'\0'),c为1(隐式'\0').a,出于某种原因,是4.
根据argc是否大于1,我希望a为9或1.我想也许字符串文字在被传递到之前转换为指针sizeof,导致sizeof(char*)为4.
我尝试替换STRING和""char数组...
char x[] = "";
char y[] = "a string";
int a = sizeof(argc > 1 ? x : y);
Run Code Online (Sandbox Code Playgroud)
...但我得到了相同的结果(a = …
首先,我已经找到了 一些可能回答我问题的参考文献.虽然我计划很快(即下班后)阅读它们,但我仍然会在这里询问,以防答案是微不足道的,并且不需要太多的补充知识.
下面是这样的情况:我正在编写一个共享库(让我们称之为libA.so),它需要在同一个进程中维护一个连贯的内部(如在.c文件中声明的静态变量)状态.程序P将使用该库(即P编译-lA).如果到目前为止我理解了所有内容,P的地址空间将如下所示:
______________
| Program P |
| < |
| variables, |
| functions |
| from P |
| > |
| |
| < |
| libA: |
| variables, |
| functions |
| loaded (ie |
| *copied*) |
| from shared |
| object |
| > |
| < |
| stuff from |
| other |
| libraries |
| > |
|______________|
Run Code Online (Sandbox Code Playgroud)
现在P有时会打电话dlopen("libQ.so", ...).libQ.so也使用libA.so(即编译时 …
我正在尝试使用 Go 中的 RPC 调用来创建一个最小的应用程序。我大量借鉴了在线示例,您可以从我的代码中看到:
服务器.go:
package main
import (
[...]
)
type InfoDumper int
func (s *InfoDumper) Dump(request string, reply *string) error {
fmt.Println("Woooh imma deliverin stuff\n")
current_time := time.Now()
h:= sha1.New()
var barray []byte
copy(barray, request)
hash_rq := h.Sum(barray)
*reply = request + "\n" + current_time.Format(time.ANSIC) + "\n" + string(hash_rq) + "\n"
return nil
}
func main() {
server := new(InfoDumper)
rpc.Register(server)
rpc.HandleHTTP()
l, e := net.Listen("tcp", "127.0.0.1:40000")
if e != nil {
fmt.Println(e)
}
http.Serve(l, …Run Code Online (Sandbox Code Playgroud)