数组的类或结构。 [英] Class or structure for array.

查看:78
本文介绍了数组的类或结构。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


如果你有一个数组,其中一些类作为元素类型和一个相同大小的数组

,其中一些结构作为元素类型,并且类和结构有相同的字段(例如10个整数),假设带有类的

数组使用更少的内存(比带有结构的数组)是正确的初始化对象之前
(每个对象X个字节)并在初始化所有对象后使用更多内存

(相同字段+每个对象X个字节)? />

谢谢。

Hello,

if you have an array with some class as element type and a same sized array
with some structure as element type, and that class and structure have the
same fields (for example 10 integers), is it correct to assume that the
array with the classes uses less memory (than the array with structures)
before the objects are initialized (X bytes per object) and uses more memory
after all the objects are initialized (same fields + X bytes per object) ?

Thanks.

推荐答案

" Qwert" <无** @ nosp.com> schrieb:
"Qwert" <no**@nosp.com> schrieb:
如果你有一个数组,其中一些类作为元素类型和一个相同大小的
数组,其结构为元素类型,并且该类和结构具有相同的字段(例如10个整数),假设具有类的数组在初始化对象之前使用较少的内存(比具有
结构的数组)(每个对象X个字节)并且字节)?
if you have an array with some class as element type and a same sized
array with some structure as element type, and that class and structure
have the same fields (for example 10 integers), is it correct to assume
that the array with the classes uses less memory (than the array with
structures) before the objects are initialized (X bytes per object) and
uses more memory after all the objects are initialized (same fields + X
bytes per object) ?




结构数组使用的内存多于类实例数组

其项目指向''Nothing''。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://classicvb.org/petition/>



An array of structures uses more memory than an array of class instances
whose items point to ''Nothing''.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


嗨Qwert,

我同意Herfried。结构是一个用户定义的数据类型只是

喜欢类,但区别是结构不需要

instanciated。因此,结构对象在堆栈内存上维护引用不同于堆内存中那些维护引用的类。

堆栈内存与堆内存相比是有限的,因此结构

在获取引用时会在堆栈中占用更多内存。这就是为什么

结构比上课要贵得多。


有更多与记忆相关的理由,但简而言之,

结构在实例化时会占用大量内存。

hi Qwert,
i''m agree with Herfried. structure is a user definded data type just
like class but the difference is structure doesnt required to be
instanciated. thus structure objects are maintaining references on
stack memory unlike classes those maintaines references in heap memory.
stack memory is limited in comparison of heap memory so that structures
occupies more memory in stack when they get references. that''s why
structure is quite costlier than class.

there are more reasons related to memory but to cut the story in short,
structure eats lots of memory when they get instanciated.


因此结构对象在堆栈上维护引用记忆


我不确定你到底是什么意思,但是没有涉及值类型(结构)的

引用。当你有一个结构的

数组时,元素是在

数组对象中内联堆的堆,而不是在堆栈上。


结构在实现时会占用大量内存。
thus structure objects are maintaining references on stack memory
I''m not sure exactly what you mean by that, but there are no
references involved for value types (Structures). And when you have an
array of the structure the elements are heap allocated inline in the
array object, not on the stack.

structure eats lots of memory when they get instanciated.




他们吃了确切地说他们的成员的大小(加上

对齐的填充),不多也不少。与你的每个对象开销很小的类相比,它们相当便宜。

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



They "eat" exactly the size of their members (plus padding for
alignment), no more and no less. Compared to classes where you have a
small per-object overhead, they are quite cheap.
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


这篇关于数组的类或结构。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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