列表中的double对象和float对象的大小是相等的
size of double and float objects in a list are equal?
我想知道float和double对象的大小是否从std::list的角度相等?
我在std::list中分配了500万个Real(别名float或double)对象,并使用Valgrind来监控内存使用情况。
在这两种情况下使用的内存是相等的,尽管'double'(8字节)的大小是'float'对象(4字节)的两倍!
Btw,当我使用'new'操作符为相同数量的对象分配内存时,双精度数组的内存使用量是浮点数组使用量的两倍,这似乎是正确的。
我使用的是gcc 4.6.2,在Fedora 16.x86_64.
任何能帮助我解开这个谜团的想法都很感激。
下面是我为test 写的代码#include <iostream>
#include <list>
typedef double Real;
int main(int argc, char** argv)
{
std::list<Real> pts;
int k;
int npts = 5000000; // 5 mil
std::cout << "sizeof(Real): " << sizeof(Real) << std::endl;
for(k=0; k < npts;++k)
pts.push_back(1.0);
return 0;
}
如果我定义Real <- double,则Valgrind输出为
==15335== Memcheck, a memory error detector
==15335== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==15335== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==15335== Command: /home/soheil/Workspace/tbin/test_memory_usage
==15335==
sizeof(Real): 8
==15335==
==15335== HEAP SUMMARY:
==15335== in use at exit: 616 bytes in 6 blocks
==15335== total heap usage: 5,000,053 allocs, 5,000,047 frees, 120,015,245 bytes allocated
==15335==
==15335== LEAK SUMMARY:
==15335== definitely lost: 0 bytes in 0 blocks
==15335== indirectly lost: 0 bytes in 0 blocks
==15335== possibly lost: 0 bytes in 0 blocks
==15335== still reachable: 616 bytes in 6 blocks
==15335== suppressed: 0 bytes in 0 blocks
==15335== Rerun with --leak-check=full to see details of leaked memory
==15335==
==15335== For counts of detected and suppressed errors, rerun with: -v
==15335== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
如果我定义Real <- float,则Valgrind输出为
==15252== Memcheck, a memory error detector
==15252== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==15252== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==15252== Command: /home/soheil/Workspace/tbin/test_memory_usage
==15252==
sizeof(Real): 4
==15252==
==15252== HEAP SUMMARY:
==15252== in use at exit: 616 bytes in 6 blocks
==15252== total heap usage: 5,000,053 allocs, 5,000,047 frees, 120,015,245 bytes allocated
==15252==
==15252== LEAK SUMMARY:
==15252== definitely lost: 0 bytes in 0 blocks
==15252== indirectly lost: 0 bytes in 0 blocks
==15252== possibly lost: 0 bytes in 0 blocks
==15252== still reachable: 616 bytes in 6 blocks
==15252== suppressed: 0 bytes in 0 blocks
==15252== Rerun with --leak-check=full to see details of leaked memory
==15252==
==15252== For counts of detected and suppressed errors, rerun with: -v
==15252== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
std::list<T>
中的每个元素都是一个链表节点,所以它是一个包含两个指针的结构体,以及T
类型的有效负载数据。例如,对于GCC 4.1.2,如下所示:
struct _List_node_base
{
_List_node_base* _M_next;
_List_node_base* _M_prev;
// *** Non-virtual member functions ***
};
template<typename _Tp>
struct _List_node : public _List_node_base
{
_Tp _M_data;
};
分配的大小将是该结构体的大小;如果T
足够小,那么你可能会看到由结构体填充主导的图形。
所以在GCC定义中,这是两个64位指针(所以16字节),加上4或8字节的T
,填充到8字节,所以总共24字节,这与您正在测量的内容相匹配。
要测试该理论,请尝试将Real
更改为float[2]
或double[2]
。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 调用的对象类型 'double' 不是 report() 函数的函数或函数指针
- 可选<double>可以作为 8 字节对象实现吗?
- 无法写入矩阵<向量<向量<double>>对象
- 对类似"struct {double, int}"对象使用reinterpret_cast进行对象访问
- 如何将C 对象(double *)返回为r作为SEXP
- 在向量中存储模板化对象(在<int><double>单个向量中存储类、类)
- 如何在 C# 中从 com 对象返回数组(double[])
- 为什么用double&&初始化对象方法不起作用?
- 列表中的double对象和float对象的大小是相等的
- 从文件中读取数据类型string, int double.将每一行数据放入对象向量中
- 创建一百万个对象,每个对象有12个double
- 错误:被调用的对象类型 'double' 不是函数或函数指针
- C/ c++: do内置对象,如' int ' ' double '在程序运行期间始终保持在内存中的确切位置
- 对象方法中的Double不接受小数值
- BinaryFormatter在转换Int32和Double等内部对象时出现问题