在struct上使用sizeof运算符? [英] Using the sizeof operator on a struct?

查看:59
本文介绍了在struct上使用sizeof运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何总结memmory自定义树结构

占用memmory?


示例:


struct node {

struct node * parent;

unsigned int nChildCount;

string folder; //字符串类

};


节点* pNode =新节点;


使用sizeof(& ; pNode)返回4.然而,我感兴趣的是

知道结构实例的整个memmory使用。

我是否必须实现递归每个节点的getSize()方法

包括两个struct指针的大小,unsigned int和

字符串对象?


提前致谢,

Casper

How would one go about summing up the memmory custom tree structures
occupies in memmory?

Example:

struct node {
struct node *parent;
unsigned int nChildCount;
string folder; //string class
};

node* pNode = new node;

Using the sizeof(&pNode) returns 4. What I am interested in however, is
to know the entire memmory use of an instance of a struct.
Will I have to implement a recursive getSize() method for each node to
include the size of the two struct pointers, the unsigned int and the
string object?

Thanks in advance,
Casper

推荐答案



" Casper" < CA **** @ jbr.dk> skrev i en meddelelse

新闻:Kz ********************* @ wagner.videotron.net。 ..

"Casper" <ca****@jbr.dk> skrev i en meddelelse
news:Kz*********************@wagner.videotron.net. ..
如何总结memmory自定义树结构
占用memmory?

示例:

结构节点{
struct node * parent;
unsigned int nChildCount;
字符串文件夹; //字符串类
};

节点* pNode =新节点;

使用sizeof(& pNode)返回4.


所以你的架构指针是四个字节。

然而,我感兴趣的是知道结构实例的整个memmory使用。
字符串对象?


Nope ... sizeof(node)返回一个节点所需的字节数。这个

是一个编译时的值,当然,它没有告诉您初始化节点的实际存储要求。

谢谢提前,
Casper
How would one go about summing up the memmory custom tree structures
occupies in memmory?

Example:

struct node {
struct node *parent;
unsigned int nChildCount;
string folder; //string class
};

node* pNode = new node;

Using the sizeof(&pNode) returns 4.
So pointers on your architecture are four bytes.
What I am interested in however, is
to know the entire memmory use of an instance of a struct.
Will I have to implement a recursive getSize() method for each node to
include the size of the two struct pointers, the unsigned int and the
string object?
Nope... sizeof(node) returns the number of bytes required by one node. This
is a compile-time value, of course, telling you nothing about the actual
storage requirement of an initialised node.

Thanks in advance,
Casper




/ Peter



/Peter




"卡斯帕" < CA **** @ jbr.dk>在消息中写道

新闻:Kz ********************* @ wagner.videotron.net。 ..

"Casper" <ca****@jbr.dk> wrote in message
news:Kz*********************@wagner.videotron.net. ..
如何总结memmory自定义树结构
占用memmory?

示例:

结构节点{
struct node * parent;
unsigned int nChildCount;
字符串文件夹; //字符串类
};

节点* pNode =新节点;

使用sizeof(& pNode)返回4.然而我感兴趣的是,是否知道结构实例的整个memmory使用。
我是否必须为每个节点实现递归getSize()方法以包含两个struct指针的大小,unsigned int和
字符串对象?

提前致谢,
Casper
How would one go about summing up the memmory custom tree structures
occupies in memmory?

Example:

struct node {
struct node *parent;
unsigned int nChildCount;
string folder; //string class
};

node* pNode = new node;

Using the sizeof(&pNode) returns 4. What I am interested in however, is
to know the entire memmory use of an instance of a struct.
Will I have to implement a recursive getSize() method for each node to
include the size of the two struct pointers, the unsigned int and the
string object?

Thanks in advance,
Casper




你需要sizeof(节点)*节点数。


您可以通过动态跟踪它来获得节点数量

您添加/删除节点或者您可以计算它可以通过完整的

遍历树来进行点播。


你将无法在编译时知道树的大小,因为

它的大小在编译时没有固定(sizeof严格来说是一个
编译时工具)。



You need sizeof(node) * number of nodes.

You could get number of nodes by either keeping track of it dynamically as
you add / delete nodes or you could calculate it on-demand with a full
traversal of the tree.

You''ll never be able to know the size of the tree at compile time because
it''s size is not fixed at compile time (and sizeof is strictly a
compile-time facility).

没有...... sizeof(node)返回一个节点所需的字节数。当然,这是一个编译时的值,它不会告诉您初始化节点的实际存储要求。
Nope... sizeof(node) returns the number of bytes required by one node. This
is a compile-time value, of course, telling you nothing about the actual
storage requirement of an initialised node.




Ah sizeof只在编译时进行评估,那对我来说不会有太大的帮助。我真正感兴趣的是监视我的树的内存使用情况

作为查看优化效果的主要指标。

无法在程序中跟踪记忆吗?


Casper



Ah sizeof is only evaluated at compile time, it won''t be of much help to
me then. All I really is interested in is monitoring the memmory usage
of my tree as the primary metric to see the effect of optimizations. Is
there no way to keep track of memmory in the program itself?

Casper


这篇关于在struct上使用sizeof运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆