使用对象或指向对象的指针作为类成员和内存分配 [英] Usage of objects or pointers to objects as class members and memory allocation

查看:149
本文介绍了使用对象或指向对象的指针作为类成员和内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里提出了一个类似的问题:



作为对象的类成员 - 指针还是不是? C ++



所以我会保持简短。



三个stl载体。我的想法是正确的,如果他们是类的常规成员,他们的记忆将是一起的整个对象?例如我的内存看起来像10块向量A,5块向量B,然后15块向量C.然后一旦我插入更多的对象到向量A中,使得空间用完整个结构,包括向量B和C需要



那么这是指针的参数吗?还是内部的向量只是指向分配的内存的指针?相同的问题会去列表等...



有关重定向的成本和复制小对象的成本有什么规则吗?

解决方案

div>


假设我有一个包含三个stl向量的对象。我的想法是正确的,如果他们是类的常规成员,他们的记忆将是一起的整个对象?例如我的记忆将看起来像10块矢量A,5块矢量B,然后15块矢量C.


每个 vector 在包含对象中占据固定大小,与向量当前存储的元素数无关。可能的是向量 value_type (例如 vector< int> 具有 value_type int )不会影响所包含的向量的大小对象本身:只有堆分配存储的向量需要为了保持其存储容量(所以他们是likey 8或16或32个字节,但都相同,但不是10块(任何可能的),5块和15)。


那么,一旦我将更多的对象插入向量A,以便空间用完整个结构,包括向量B和C需要移动?


将元素插入 A 只能使 A 中的现有元素移动超过)。 B C 永远不会受到影响。


那么是指针的参数吗?或者是内部只有指向分配的内存的指针?


是,这是一个参数...这样一个好的, 向量已经 do 使用指向实际 value_type 元素存储的连续内​​存的指针。


同样的问题会用于列表等...


是,列表将它们的 value_type 元素存储在堆上,对象嵌入或派生自 list 不受列表上的操作影响。


有关重定向的成本和复制小对象的成本有什么规则吗?也许沿着5指针重定向行= 1整数复制?在这里输入代码


C ++运行在太多的平台上,因此有很好的经验法则。即使在x86处理器上,指令集,#内核,高速缓存大小,CPU供应商/模型/生成等方面的差异可能是压倒性的。间接性是最昂贵的,如果它导致内存页错误,这是非常依赖于机器上的程序执行的整体情况。如果你关心,运行程序的基准真实计算机,直到你找到统计上相关和稳定的结果。


A similar question has been asked here:

Class members that are objects - Pointers or not? C++

so I'll keep it brief.

Say I have an object that contains three stl vectors. Is my thinking correct that if they are regular members of the class, the memory of them will be "together" for the whole object? e.g. my memory would look sth like 10 blocks vector A, 5 blocks vector B, and then 15 blocks vector C. Would then once I insert more objects into vector A so that the space runs out the whole structure including vector B and C need to be moved?

Is that then an argument for pointers? Or are vectors internally only a pointer to the allocated memory? Same question would go for lists etc...

Are there any rules of thumb as to the cost of a redirection vs the cost of copying small objects? Maybe sth along the lines of 5 pointer redirection = 1 integer copy?

Thanks

解决方案

Say I have an object that contains three stl vectors. Is my thinking correct that if they are regular members of the class, the memory of them will be "together" for the whole object? e.g. my memory would look sth like 10 blocks vector A, 5 blocks vector B, and then 15 blocks vector C.

Each vector occupies a fixed size in the containing object, independent of the number of elements currently stored by the vector. It is likely that the value_type of the vector (e.g. vector<int> has value_type int) won't affect the size of the contained vector object itself: only the amount of heap-allocated store the vector needs in order to maintain its storage capacity (so they're likey to be say 8 or 16 or 32 bytes each but all the same, but not 10 "blocks" (whatever that might be), 5 blocks and 15).

Would then once I insert more objects into vector A so that the space runs out the whole structure including vector B and C need to be moved?

Inserting elements into A can only ever cause existing elements in A to be moved (when the capacity is exceeded). B and C can never be affected.

Is that then an argument for pointers? Or are vectors internally only a pointer to the allocated memory?

YES, it's an argument... such a good one that YES, vectors already do use pointers to the contiguous memory where the actual value_type elements are stored.

Same question would go for lists etc...

YES, lists store their value_type elements on the heap too, and the size of the object embedding or derived from list is unaffected by operations on the list.

Are there any rules of thumb as to the cost of a redirection vs the cost of copying small objects? Maybe sth along the lines of 5 pointer redirection = 1 integer copy?enter code here

C++ runs on too many platforms for there to be good rules of thumb for this. Even on say x86 processors, differences in instruction set, # cores, cache sizes, CPU vendor / model / generation etc. can be overwhelming. Indirection is most costly if it results in memory page faults, and that's very dependent on the overall picture of program execution on the machine. If you care, benchmark real computers running the program until you find statistically relevant and stable results.

这篇关于使用对象或指向对象的指针作为类成员和内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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