关闭

Reference variable is not allocated memory

334人阅读 评论(0) 收藏 举报

Here is a memory map; a representation of memory as a sequence of blocks:

    address    01   02   03
             +----+----+----+...
data within  | 23 | 6f | 4a |
             +----+----+----+...

Now suppose we create a character:

char c = 'z';  // 'z' is 7a in hex

Further suppose c is stored at address 01, so our memory looks like so:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 6f | 4a |
             +----+----+----+...
Now, let's create a pointer:
char* p = &c;  // point at c

p may be stored at address 02:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 01 | 4a |
             +----+----+----+...
 

Here the pointer p is at address 02 and it points at address01. That's the meaning ofp = &c;. When we dereference the pointerp (at address02) we look at what's in the address pointed at byp. That is,p points at address 01, and so dereferencingp means looking inside address01.

Finally, lets create a reference:

char& r = c;

Here the memory layout doesn't change. That is, no memory is used to store r. r is a sort of alias for c, so when we refer tor we practically refer toc. r and c are conceptually one. Changingr means changingc, and changing c means changingr.

When you create a reference you must initialize, and once initialized you cannot re-initialize it with another target. That is, above the referencer means and forever will meanc.

Also related are const references. These are the same as a reference, except they are immutable:

const char& r = c;
r = 'y';  // error; you may not change c through r
c = 'y'   // ok. and now r == 'y' as well

We use const references when we are interested in reading the data but frown upon changing it. By using a const reference the compiler will not copy the data, so this gives us ideal performance, but also forbid us from changing the data, for correctness.

In a sense, you can say that references are a compile-time feature, whereas pointers are a runtime feature. So references are faster and cheaper than pointers, but come with certain constraints and implications. Like other compile-time-vs-runtime alternatives, we sometimes pick one over the other for performance, sometimes for static analysis and sometimes for flexibility.

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:601198次
    • 积分:9011
    • 等级:
    • 排名:第2069名
    • 原创:259篇
    • 转载:289篇
    • 译文:1篇
    • 评论:102条
    最新评论
    数据结构