gpt4 book ai didi

c++ - 列表中 double 和 float 对象的大小是否相等?

转载 作者:IT老高 更新时间:2023-10-28 23:19:04 24 4
gpt4 key购买 nike

我想知道从 std::list 的角度来看 float 和 double 对象的大小是否相等?

我在 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;

}

如果我定义 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)

如果我定义 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] .

关于c++ - 列表中 double 和 float 对象的大小是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8475989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com