小编A. *_*Ite的帖子

在C中访问另一个程序的变量

在python中,您可以使用id函数学习变量的内存位置,因此:

X = "Hello world!"
print(id(X)) # Output is equal to 139806692112112 (0x7F27483876F0)
Run Code Online (Sandbox Code Playgroud)

我试图用C中的指针访问变量(当然其他程序仍然存在):

#include <stdio.h>

int main(void){
    char *x = (char *) 0x7F27483876F0;
    printf("%s\n", x);

    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

我编译代码,没有错误或警告,但当我尝试运行程序操作系统给出分段错误.我怎么能解决这个问题?

或者有可能吗?

c c++ python

6
推荐指数
1
解决办法
214
查看次数

为什么C/C++比Assembly和其他低级语言慢?

我编写了一个代码,在C++中什么都不做

void main(void){

}
Run Code Online (Sandbox Code Playgroud)

和大会.

.global _start
.text

_start:
    mov $60, %rax
    xor %rdi, %rdi 
    syscall
Run Code Online (Sandbox Code Playgroud)

我编译C代码并编译和链接汇编代码.我用time命令比较了两个可执行文件.

部件

time ./Assembly

real    0m0.001s
user    0m0.000s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

C

time ./C

real    0m0.002s
user    0m0.000s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

汇编速度比C快两倍.我反汇编代码,在汇编代码中,只有四行代码(相同).在C代码中,有大量不必要的代码用于将main链接到_start.主要有四行代码,其中三行代表不可能(你不能从函数博客外部访问函数的变量)从' block ' 外部访问' local '(如函数变量)变量(像功能块一样).

push %rbp ; push base pointer.
mov  %rsp, %rbp ; copy value of stack pointer to base pointer, stack pointer is using for saving variables.
pop  %rbp ; 'local' variables are removed, because we pop the base …
Run Code Online (Sandbox Code Playgroud)

c++ assembly

-27
推荐指数
1
解决办法
690
查看次数

标签 统计

c++ ×2

assembly ×1

c ×1

python ×1