Soh*_*deh 12 c++ floating-point memory-management stl list
我想知道浮动和双重对象的大小是否从std :: list的角度来看是相等的?
我在std :: list中分配了500万个Real(别名float或double)对象,并使用Valgrind监视内存使用情况.
在这两种情况下,使用的内存是相等的,尽管'double'(8字节)的大小是一个'float'对象(4字节)的大小的两倍!
顺便说一句,当我使用'new'运算符为相同数量的对象分配内存时,double数组的内存使用量是float数组的两倍,这似乎是正确的.我也期待使用std :: list.
我在Fedora 16.x86_64上使用gcc 4.6.2.
有任何想法,以帮助我了解这个谜.
这是我为测试编写的代码
#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;
}
Run Code Online (Sandbox Code Playgroud)
如果我定义Real < - 双重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)
Run Code Online (Sandbox Code Playgroud)
如果我定义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)
Run Code Online (Sandbox Code Playgroud)
Oli*_*rth 10
a中的每个元素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;
};
Run Code Online (Sandbox Code Playgroud)
分配的大小将是该结构的大小; 如果T足够小,那么你可能会看到由struct padding主导的数字.
因此,对于GCC定义,这是两个64位指针(如此16个字节),加上4或8个字节T,最多填充8个字节,因此总共24个字节,与您测量的内容相匹配.
为了测试理论,尝试Real改为float[2]或double[2].