关于对象定义和初始化的理解

shycpp 2003-08-20 10:53:39
下面对对象定义和初始化的理解是否正确?
1) 当我们定义了一个变量,无论其是否初始化,编译器是否都对其进行了内存分配吧,如:
int dvar1;
int dvar2 = 236;
当编译器遇到这两个语句后,在编译阶段会为变量dvar1、dvar2分配内存,对否?
...全文
75 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
langhaixin 2003-08-20
  • 打赏
  • 举报
回复
变量初始化的责任是由程序员来承担的,并不是由编译器来承担的。
变量是否有初值,初值是什么这完全是程序员个人的需求,对于编译器来说,他并不强制要求一个变量有初值。

对于C++来说,内存应该有6个部分

Const Data The const data area stores string literals and
other data whose values are known at compile
time. No objects of class type can exist in
this area. All data in this area is available
during the entire lifetime of the program.

Further, all of this data is read-only, and the
results of trying to modify it are undefined.
This is in part because even the underlying
storage format is subject to arbitrary
optimization by the implementation. For
example, a particular compiler may store string
literals in overlapping objects if it wants to.


Stack The stack stores automatic variables. Typically
allocation is much faster than for dynamic
storage (heap or free store) because a memory
allocation involves only pointer increment
rather than more complex management. Objects
are constructed immediately after memory is
allocated and destroyed immediately before
memory is deallocated, so there is no
opportunity for programmers to directly
manipulate allocated but uninitialized stack
space (barring willful tampering using explicit
dtors and placement new).


Free Store The free store is one of the two dynamic memory
areas, allocated/freed by new/delete. Object
lifetime can be less than the time the storage
is allocated; that is, free store objects can
have memory allocated without being immediately
initialized, and can be destroyed without the
memory being immediately deallocated. During
the period when the storage is allocated but
outside the object's lifetime, the storage may
be accessed and manipulated through a void* but
none of the proto-object's nonstatic members or
member functions may be accessed, have their
addresses taken, or be otherwise manipulated.


Heap The heap is the other dynamic memory area,
allocated/freed by malloc/free and their
variants. Note that while the default global
new and delete might be implemented in terms of
malloc and free by a particular compiler, the
heap is not the same as free store and memory
allocated in one area cannot be safely
deallocated in the other. Memory allocated from
the heap can be used for objects of class type
by placement-new construction and explicit
destruction. If so used, the notes about free
store object lifetime apply similarly here.


Global/Static Global or static variables and objects have
their storage allocated at program startup, but
may not be initialized until after the program
has begun executing. For instance, a static
variable in a function is initialized only the
first time program execution passes through its
definition. The order of initialization of
global variables across translation units is not
defined, and special care is needed to manage
dependencies between global objects (including
class statics). As always, uninitialized proto-
objects' storage may be accessed and manipulated
through a void* but no nonstatic members or
member functions may be used or referenced
outside the object's actual lifetime.
eric8231 2003-08-20
  • 打赏
  • 举报
回复
如果dvar1是全局变量,int dvar1;就相当于int dvar1=0; 这时编译器会在编译阶段在datasegment中为dvar1分配储存。

如果dvar1是局部变量,在程序运行时系统会在栈中为dvar1分配储存。
Smartdoggie 2003-08-20
  • 打赏
  • 举报
回复
1.不会
2.执行阶段
3.程序的内存分为三个部分:
静态储存区, //如你所言
管理自动变量的栈, //自动变量在执行期需要的时候才会被分配内存
以及程序员自己管理的内存池 //就是为new 分配的内存空间

shycpp 2003-08-20
  • 打赏
  • 举报
回复
编译器到底都完成了什么功能?望高手系统总结!
TIA
shycpp 2003-08-20
  • 打赏
  • 举报
回复
2) 谭浩强在“C语言程序设计第二版”P53中说:“初始化不是在编译阶段完成的(只有静态存储变量和全局变量的初始化是在编译阶段完成的),而是在程序运行时执行本函数(好像应该是本语句)时赋予初值的,相当于一个赋值语句”。这句话正确与否?C++也这样吗?那局部变量/对象内存分配发生在那个阶段(编译阶段 or 执行阶段)?
3)是否可以理解编译阶段的内存分配只是指在程序数据区进行的内存分配,如全局对象、静态对象等,而在栈空间中分配的对象是在程序执行阶段分配内存?
shycpp 2003-08-20
  • 打赏
  • 举报
回复
那么对象的初始化和定义/声明一个对象后对对象赋值在实现上有何区别?都是先分配存储空间,而后把初值赋给该存储空间。既然这样,为什么类中的常量赋初值的时候必须用初始化符表。

64,637

社区成员

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

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