在运行时说,我想找出定义函数"printf"的位置.我该怎么做?我的第一次尝试是打印出"printf"的地址,并将其与进程的虚拟地址映射进行比较:
我的节目:
#include <stdio.h>
#include <unistd.h>
void main()
{
printf("address of printf is 0x%X\n", printf);
printf("pid is %d\n", getpid());
while (1);
}
Run Code Online (Sandbox Code Playgroud)
输出:
-bash-4.1$ ./a &
[1] 28837
-bash-4.1$ address of printf is 0x4003F8
pid is 28837
Run Code Online (Sandbox Code Playgroud)
但是,这表示该功能是在我自己的程序中定义的!
-bash-4.1$ head /proc/28837/maps
00400000-00401000 r-xp 00000000 08:06 6946857 /data2/temp/del/a <<<<<<< Address 0x4003F8 is in my own program?
00600000-00601000 rw-p 00000000 08:06 6946857 /data2/temp/del/a
397ec00000-397ec20000 r-xp 00000000 08:11 55837039 /lib64/ld-2.12.so
397ee1f000-397ee20000 r--p 0001f000 08:11 55837039 /lib64/ld-2.12.so
397ee20000-397ee21000 rw-p 00020000 08:11 55837039 /lib64/ld-2.12.so
397ee21000-397ee22000 …Run Code Online (Sandbox Code Playgroud) 这是defaultdict的工作原理:
from collections import defaultdict
a=defaultdict(lambda:3)
a[200]==3 #True
Run Code Online (Sandbox Code Playgroud)
为什么它defaultdict被设计为采用没有参数的函数,而不仅仅是一个常量值?
这是另一种定义.
class dd(dict):
def __init__(self,x):
self._default=x
def __getitem__(self,key):
if key in self: return dict.__getitem__(self, key)
else:
self[key]=self._default
return self[key]
Run Code Online (Sandbox Code Playgroud)
以便
a=dd(3)
a[200]==3 #True
Run Code Online (Sandbox Code Playgroud) 我有一组长时间运行的测试,用构建标签定义。例如,
// file some_test.go
//+build func_test
(rest of file with test cases)
Run Code Online (Sandbox Code Playgroud)
我还有许多其他较短的运行测试,没有这个构建标志。有没有一种方法可以轻松地只运行包含构建标签“func_test”的测试?
请注意,如果我只是运行go test -tags func_test,它会运行所有测试,包括some_test.go.
这是我找到的代码
static_assert(sizeof(struct File) == 256);
Run Code Online (Sandbox Code Playgroud)
宏的定义是这样的:
#define static_assert(x) switch (x) case 0: case (x):
Run Code Online (Sandbox Code Playgroud)
我正在运行x86,我想看到我的机器上无序执行导致的错误.根据这篇维基文章,我尝试写一篇,但我总是看到"x的值是33":
#include<stdio.h>
#include<pthread.h>
#include <sys/types.h>
int x, f;
void *handler(void *ptr) {
while (f == 0);
// Expectation: Sometimes, this should print 11 due to out-of-order exec
printf("value of x is %d \n", x);
return NULL;
}
int main() {
pthread_t thread1;
while(1) {
x = 11; f = 0;
pthread_create(&thread1, NULL, handler, NULL);
x = 33;
f = 1;
pthread_join(thread1, NULL);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
什么是最简单的c程序可以说明乱序执行错误?为什么这有时不打印"x的值是11"?
我想知道当代码为%esp设置值时,汇编程序如何计算“ bootstacktop”和“ bootstack”的值:
# Set the stack pointer
movl $(bootstacktop),%esp
Run Code Online (Sandbox Code Playgroud)
在同一汇编文件的末尾,给出了“ bootstacktop”的“定义”:
###################################################################
# boot stack
###################################################################
.p2align PGSHIFT # force page alignment
.globl bootstack
bootstack:
.space KSTKSIZE
.globl bootstacktop
bootstacktop:
Run Code Online (Sandbox Code Playgroud)
我找到了值“ destackbly”中的“ bootstacktop”,这是上述“ mov”指令的反汇编的一部分:
# Set the stack pointer
movl $(bootstacktop),%esp
f0100034: bc 00 40 11 f0 mov $0xf0114000,%esp
Run Code Online (Sandbox Code Playgroud)
KSTKSIZE的值为8 * 4096,PGSHIFT为12。'bootsacktop'的值如何变为'0xf0114000'?那么“ bootstack”的价值是什么?
这是链接描述文件:http : //pastebin.com/9DPakfgx
在以下代码中:
int main(void) {
printf("before child\n");
int pid = fork();
if(pid == 0)
{
exit(0);
}
int status;
wait(&status);
if(4 != printf("abc\n"))
perror("printing to stdout\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产生输出:
before child
abc
Run Code Online (Sandbox Code Playgroud)
对子进程中的exit()的调用应该关闭所有文件描述符,包括stdout fd.那么父进程如何在关闭之后仍然写入stdout?