gpt4 book ai didi

c++ - 找出结构的内存消耗

转载 作者:行者123 更新时间:2023-11-28 01:23:05 25 4
gpt4 key购买 nike

我想弄清楚我的结构将占用多少内存。考虑以下代码

   struct tNode{
short data;
tnode *left;
tnode *right;
tnode *student;
}

所以我知道 data 是 2 个字节,但是 *left*right*student 呢,他们将消耗多少内存以及如何计算它。

最佳答案

您正在寻找 sizeof operator

Returns size in bytes of the object representation of type

示例用法:

#include <iostream>

class tnode;

struct tNode {
short data;
tnode *left;
tnode *right;
tnode *student;
};

int main()
{

std::cout << sizeof(tNode) << std::endl;
return 0;
}

我机器上的输出:

32

关于c++ - 找出结构的内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55207550/

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