列表中的double对象和float对象的大小是相等的

size of double and float objects in a list are equal?

本文关键字:对象 double 列表 float      更新时间:2023-10-16

我想知道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]