Is primitive assigned a memory address?

依然范特西╮ 提交于 2019-12-01 05:12:48

There are not always addresses involved. The compiler can put variables into registers if it finds that their address is never taken by the programmer. So you wouldn't need any access to the main memory. For example in your code above, what the compiler could generate could be as simple as

add $2, $0, 3

to put value 3 into register 2. As soon as you create a pointer and make it point to that variable, then you have an address, actually. And then the variable cannot be in a register only anymore.

Assuming you're talking about C or C++ (I can't tell), yes. You can access the address like so:

int i = 3;

int *k = &i; // k now is a pointer to i

*k = 4; // assigns the value k points to (i) to 4, i is now 4

How else do stack buffer overflows occur? :) someone's got to be writing to a pointer to the stack.

Really stack is special area of proccess virtual memory, so everything in stack has memory addres. Head of the stack holded by ESP (SP) registry (x86 architecture). Stack addresses usualy lower than memory adresses, because stack located closer to begining of proccess vitrual memory then heap.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!