对象的内存使用情况 [英] memory usage of objects

查看:87
本文介绍了对象的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想知道以下情况下的内存使用情况的详细信息.
1.假设创建了10个相同类的对象.所有这些对象都共享相同的内存吗?
2.在C中,我们将整数声明为"int i;".在C ++中,当我们将"i"声明为成员变量时,"int i"和成员变量"i"都具有相同的大小吗?

Hi,
I would like to know the details of memory usage under the following scenarios.
1. Suppose 10 objects are created of the same class. Will all these objects share the same memory?
2. In C, we declare an integer as "int i;". In C++ , when we declare "i" as member variable, both "int i" and member variable "i" take the same size?

Is there any tutorial/ link for understanding these basics?

推荐答案

1.否.它们是内存中的单独实例.
2.是的.即使填充和对齐方式可能有所不同.
1. No. They are separate instances in memory.
2. Yes. Even though padding and alignment might be different.


1.假设创建了10个相同类的对象.所有这些对象会共享相同的内存吗?
没有类对象不会共享内存.每个对象都有自己的内存分配.但是,类的静态成员将由其所有对象共享.

2.在C中,我们将整数声明为"int i;".在C ++中,当我们声明"i"作为成员变量时,"int i"和成员变量"i"都具有相同的大小吗?
是两者将具有相同的内存大小.

链接:
1. http://www.cplusplus.com/doc/tutorial/ [
1.Suppose 10 objects are created of the same class. Will all these objects share the same memory?
No the class objects wont share the memory.Each object will have its own memory allocation.But, Static member of a class will be shared by all its objects.

2.In C, we declare an integer as "int i;". In C++ , when we declare "i" as member variable, both "int i" and member variable "i" take the same size?
Yes Both will have same memory size.

Links:
1. http://www.cplusplus.com/doc/tutorial/[^]

2. Use a ANSI compiler and learn debugging.This will help you to clarify this types of doubts


1)类是具有函数和成员变量的模板或蓝图.对象是类的实例.当我们创建一个对象时,会为该对象分配一个内存.此内存等效于成员变量的内存.如果成员变量是静态的而不是在所有对象之间共享,则例外.功能在内存中加载一次并由每个对象共享.功能共享是在"this"指针的帮助下完成的.
2)是的. int的大小始终相同. int的大小取决于编译器和处理器.

书籍:Herbert Schildt c ++完整参考书
-> [
1) Class is a template or blueprint that have functions and member variable. Object is an instance of class. When we create an object than a memory is allocated to that object. This memory is equivalent to the memory of member variable.Exception is if member variable is static than it is shared among all object.functions are loaded once in a memory and shared by every object. sharing of function is done with the help of "this" pointer.
2) Yes. The size of int is always same. The size of int depends on compiler and processor.

Book : Herbert Schildt c++ complete reference
->[^]


这篇关于对象的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆