Reference variable is not allocated memory

转载 2012年03月23日 15:01:27

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.


ssh@host:""ssh切换到其他机器上执行多条命令;Pseudo-terminal will not be allocated because stdin is not a terminal.

1.背景:shell脚本在主机器上执行kafka的启动命令,其他机器也会起这个进程。之前的脚本每次执行的时候出现卡住的现象,不能回到命令行。解决方法:第一种#/bin/bash KAFKA_HOME=...
  • wyl9527
  • wyl9527
  • 2017年05月26日 15:27
  • 1061

Variable 'X' is used before it has been assigned a value. A null reference exception could result at runtime.

         在自己做机房收费系统的过程中出现了这样的一个现象:如图                        str1 下面出现了一行波浪线.同时提醒自己错出现的错误.提示如下:  ...
  • jnqqls
  • jnqqls
  • 2011年05月07日 13:38
  • 1898

tomcat启动一闪而过解决办法报错The CATALINA_HOME environment variable is not defined correctly

tomcat启动时一闪而过解决办法: 1.查找报错原因: 针对windows环境,liunx环境未进行测试 我的tomcat安装路径为:D:\apache-tomcat-7.0.57 进入cmd控制台...

[Python]Numpy安装及一些玄学问题(is not a supported wheel on this platform、Undefined variable from import)

numpy是python的一个科学计算库,可以实现matlab里的数学运算,基本是用python做数值运算的人必装的一个的库了。 然而今天我安装这个库遇到了一些以前一直没遇到的问题,挺迷的,我之前安装...

TOMCAT-报错The BASEDIR environment variable is not defined correctly

The BASEDIR environment variable is not defined correctly 启动tomcat时报错 The BASEDIR environment varia...
  • Evils798
  • Evils798
  • 2012年02月22日 19:51
  • 29997

tomcat运行出现出现问题(the JRE_HOME environment variable is not defined correctly This environment variabl)

系统:win 8.1  前阵子电脑装了个SSD,重新配置了tomcate,今天突然报错, 显示  the JRE_HOME environment variable is not defined...

TOMCAT JAVA_HOME or JRE_HOME environment variable is not defined correctly

搭建J2EE环境的时候安装TOMACAT这一步出现了这个问题,我明明配置好了环境变量,不知道为什么仍然会出现这个错误,后来参考了这个博客进行了修改,运行成功,在此记下: http://blog.cs...

Tomcat安装后出现the JRE_HOME environment variable is not defined correctly

1.安装完Tomcat配置环境以后,如果出现以下情况: 点击tomcat\bin目录下startup批处理文件时,dos界面出现闪退情况 或者win+r输入cmd---》进入dos界面--...

the java_home environment variable is not defined correctly

tomcat是java程序运行不可或缺的一个工具,最近却遇到 一个问题,tomcat启动不了,点击启动,总是一闪而过。查资料了解到,这属于启动失败,那么应该如何查看具体问题在哪呢?     首先,可...
您举报文章:Reference variable is not allocated memory