我编写了一个小C代码,它存在一个变量作为int指针。进程应该做的是减去该变量的值,直到它达到0。 问题是,即使程序打印的地址相同,每个进程似乎都有自己的变量副本。是否会发生这种情况,因为每个进程在内存中都有自己的空间?
这是输出:
START
Account address: 0x7ffee5820be0
Parent substracted 10. Account value: 90, address 0x7ffee5820be0
Parent substracted 10. Account value: 80, address 0x7ffee5820be0
Parent substracted 10. Account value: 70, address 0x7ffee5820be0
Parent substracted 10. Account value: 60, address 0x7ffee5820be0
Parent substracted 10. Account value: 50, address 0x7ffee5820be0
Parent substracted 10. Account value: 40, address 0x7ffee5820be0
Parent substracted 10. Account value: 30, address 0x7ffee5820be0
Parent substracted 10. Account value: 20, address 0x7ffee5820be0
Parent substracted 10. Account value: 10, address 0x7ffee5820be0
Parent substracted 10. Account value: 0, address 0x7ffee5820be0
Child substracted 10. Account value: 90 , address 0x7ffee5820be0
Child substracted 10. Account value: 80 , address 0x7ffee5820be0
Child substracted 10. Account value: 70 , address 0x7ffee5820be0
Child substracted 10. Account value: 60 , address 0x7ffee5820be0
Child substracted 10. Account value: 50 , address 0x7ffee5820be0
Child substracted 10. Account value: 40 , address 0x7ffee5820be0
Child substracted 10. Account value: 30 , address 0x7ffee5820be0
Child substracted 10. Account value: 20 , address 0x7ffee5820be0
Child substracted 10. Account value: 10 , address 0x7ffee5820be0
Child substracted 10. Account value: 0 , address 0x7ffee5820be0
答案 0 :(得分:6)
是的,你的假设是正确的。
如果您想共享内存空间,可以选择以下几种方法:
答案 1 :(得分:1)
查看这篇文章,我认为它将解释为什么父母和孩子的地址似乎相同,以及父母和孩子之间不共享整个记忆状态的事实: