C++ 内存管理模式

xxhhwt 2010-06-22 03:27:22
C++中的有引用类型的变量,对于引用变量,它存储的是其他数据在内存中的地址,那么我想问一下,具体的数据是分配在内存的上面地方?对于引用变量,应该是在栈区吧,那么引用所指向的数据呢?是在堆区,还是全局数据区,或者是我理解有错误?望各位不吝赐教。

关于C++的内存模式一直都感觉掌握的很模糊,尤其是最近看了一下C#,感觉更加晕了,请大家推荐一下相关书籍或者文档,C++或者C#的内存管理方面的皆可。
...全文
640 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
macrojj 2010-06-27
  • 打赏
  • 举报
回复
不要一起看啊。
Defonds 2010-06-27
  • 打赏
  • 举报
回复
编程也需要天赋的
cswuyg 2010-06-27
  • 打赏
  • 举报
回复
一、内存区
1、全局/静态数据区
2、常量数据区
3、代码区
4、栈
5、堆
全局变量、static变量存放在1;
常量存放在2;
编译器产生的临时变量、不通过new分配空间的变量存放在4
通过new分配空间的变量存放在5
二、引用的内存空间
我只用过g++和vc编译器,据我了解引用都是占用空间的。没见过引用不占空间的编译器,有了解的楼下说明,谢谢。
我的测试代码:
////////////////////////////////////////////////
////////////////////////////////////////////////
#include<iostream>
using namespace std;
////////////////////////////////////////////////
int main()
{
int a = 10;
int &b = a;
cout<<b<<endl;
system("pause");
return 0;
}

在32位机器,VC下编译,把exe文件进行动态调试,查看内存地址。结果如下:

表明占用4个字节的空间
tiansne2010 2010-06-24
  • 打赏
  • 举报
回复
牛人

传说
paula2008 2010-06-23
  • 打赏
  • 举报
回复
原来如此
papaofdoudou 2010-06-23
  • 打赏
  • 举报
回复
每天回复,第二天即可获得10分可用分。
dadun 2010-06-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fanster28_ 的回复:]
飞雪神牛去微软了啊
[/Quote]

第一反应,撼地神牛。
G_cofa 2010-06-23
  • 打赏
  • 举报
回复
学习了。谢了。
ajysnihao 2010-06-23
  • 打赏
  • 举报
回复
每天拿点分
sallan 2010-06-23
  • 打赏
  • 举报
回复
mark
ekisstherain 2010-06-23
  • 打赏
  • 举报
回复
mark....
liutengfeigo 2010-06-22
  • 打赏
  • 举报
回复

天理何在
liutengfeigo 2010-06-22
  • 打赏
  • 举报
回复
................大4就这么神奇了?晕死。
没得比。。~
baihacker 2010-06-22
  • 打赏
  • 举报
回复
关于建立引用的引用的问题有提案:
106. Creating references to references during template deduction/instantiation
Section: unknown unknown Status: WP Submitter: Bjarne Stroustrup Date: unknown

[Moved to DR at 10/01 meeting.]

The main defect is in the library, where the binder template can easily lead to reference-to-reference situations.

Proposed resolution (04/01):

Add the following as paragraph 6 of 7.1.3 dcl.typedef:
If a typedef TD names a type "reference to cv1 S," an attempt to create the type "reference to cv2 TD" creates the type "reference to cv12" S," where cv12 is the union of the cv-qualifiers cv1 and cv2. Redundant qualifiers are ignored. [Example:

int i;
typedef int& RI;
RI& r = i; // r has the type int&
const RI& r = i; // r has the type const int&

—end example]
Add the following as paragraph 4 of 14.3.1 temp.arg.type:
If a template-argument for a template-parameter T names a type "reference to cv1 S," an attempt to create the type "reference to cv2 T" creates the type "reference to cv12 S," where cv12 is the union of the cv-qualifiers cv1 and cv2. Redundant cv-qualifiers are ignored. [Example:

template <class T> class X {
f(const T&);
/* ... */
};
X<int&> x; // X<int&>::f has the parameter type const int&

—end example]
In 14.8.2 temp.deduct paragraph 2 bullet 3 sub-bullet 5, remove the indicated text:
Attempting to create a reference to a reference type or a reference to void.
(See also paper J16/00-0022 = WG21 N1245.)
tjhack7946358 2010-06-22
  • 打赏
  • 举报
回复
原来这位就是传说中的飞雪。。
baihacker 2010-06-22
  • 打赏
  • 举报
回复
3 It is unspecified whether or not a reference requires storage (3.7).
4 There shall be no references to references, no arrays of references, and no pointers to references. The declaration
of a reference shall contain an initializer (8.5.3) except when the declaration contains an explicit
extern specifier (7.1.1), is a class member (9.2) declaration within a class declaration, or is the declaration
of a parameter or a return type (8.3.5); see 3.1. A reference shall be initialized to refer to a valid object
or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only
way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer,
which causes undefined behavior. As described in 9.6, a reference cannot be bound directly to a bit-field. ]
yunyun1886358 2010-06-22
  • 打赏
  • 举报
回复
在VC中引用应该是占用内存的
wo_happy 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 yunyun1886358 的回复:]
存储变量的内存位置与引用没有直接的关系
1、局部临时变量存储在栈区
2、new动态分配的变量存储在堆区
3、全局变量、静态变量存储在全局数据区
[/Quote]
TOP
引用(引用不占内存):
(1)&在此不是求地址运算,而是起标识作用。
(2)类型标识符是指目标变量的类型。
(3)声明引用时,必须同时对其进行初始化。
(4)引用声明完毕后,相当于目标变量名有两个名称,即该目标原名称和引用名,且不能再把该引用名作为其他变量名的别名。
ra=1; 等价于 a=1;
(5)声明一个引用,不是新定义了一个变量,它只表示该引用名是目标变量名的一个别名,它本身不是一种数据类型,因此引用本身不占存储单元,系统也不给引用分配存储单元。故:对引用求地址,就是对目标变量求地址。&ra与&a相等。
(6 ) 不能建立数组的引用。因为数组是一个由若干个元素所组成的集合,所以无法建立一个数组的别名.
(7)不能建立引用的引用,不能建立指向引用的指针。因为引用不是一种数据类型,所以没有引用的引用,没有引用的指针。
yunyun1886358 2010-06-22
  • 打赏
  • 举报
回复
存储变量的内存位置与引用没有直接的关系
1、局部临时变量存储在栈区
2、new动态分配的变量存储在堆区
3、全局变量、静态变量存储在全局数据区
刀仔割大树 2010-06-22
  • 打赏
  • 举报
回复
都是牛人。。。呵呵
加载更多回复(8)

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧