Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Using the sizeof operator on a struct? (http://www.velocityreviews.com/forums/t285176-using-the-sizeof-operator-on-a-struct.html)

Peter Koch Larsen 08-23-2004 07:39 PM

Re: Using the sizeof operator on a struct?
 

"Casper" <casper@jbr.dk> skrev i en meddelelse
news:KzrWc.77021$F85.1435225@wagner.videotron.net. ..
> 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



Dave 08-23-2004 08:01 PM

Re: Using the sizeof operator on a struct?
 

"Casper" <casper@jbr.dk> wrote in message
news:KzrWc.77021$F85.1435225@wagner.videotron.net. ..
> 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


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).



Dave 08-23-2004 08:15 PM

Re: Using the sizeof operator on a struct?
 

"Casper" <casper@jbr.dk> wrote in message
news:yasWc.78772$F85.1469873@wagner.videotron.net. ..
>
> > 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 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


There's nothing in *standard* C++ that will give you the size of a process's
memory image (and standard C++ is what this newsgroup deals with). However,
there is probably some API call specific to your platform that will do what
you want. Also, my earlier post suggests something that I think will work
for you...



Steve 08-23-2004 10:23 PM

Re: Using the sizeof operator on a struct?
 
On 23/8/04 11:31 pm, in article
KzrWc.77021$F85.1435225@wagner.videotron.net, "Casper" <casper@jbr.dk>
wrote:

> 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.



sizeof(&pNode) == sizeof(node**)
sizeof(pNode) == sizeof(node*)
sizeof(*pNode) == sizeof(node)


Does that help?

Steve.


Casper 08-23-2004 10:31 PM

Using the sizeof operator on a struct?
 
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 08-23-2004 11:12 PM

Re: Using the sizeof operator on a struct?
 

> 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 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

Casper 08-23-2004 11:29 PM

Re: Using the sizeof operator on a struct?
 
> You need sizeof(node) * number of nodes.

But how does that help me, this gives me only the total memmory of all
node pointers, not including struct member variables - in particular
when one of the members is a reference to a class (string).

Thanks for the feedback though, I will try to hunt for a solution in my
OS API!
Casper

Siemel Naran 08-24-2004 04:46 AM

Re: Using the sizeof operator on a struct?
 
"Casper" <casper@jbr.dk> wrote in message news:rZxWc.86262

> int CalcSizeOf(node* pNode)
> {
> long size = pNode->folder.GetAllocLength() * sizeof(TCHAR);
> size += sizeof(pNode->nChildCount);
> //Parent pointer AND ourself, hence x2
> size += (2 * sizeof(pNode->parent));
> return size;
> }


Don't forget the sizeof(pNode->folder) itself. Most non-reference counted
strings contain a pointer to the start of the string, pointer to the end,
and integer reflecting the capacity (what appears to be GetAllocLength in
your string class).

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

Your function calculates a long, but returns an int! Beware losing
precision.

unsigned long CalcSizeOf(node* pNode) {
unsigned long size = sizeof(pNode);
if (pNode) {
size += sizeof(*pNode);
size += pNode->folder.GetAllocLength() * sizeof(TCHAR);
}
return size;
}

And what if your string class is reference counted? Then sizeof(string) is
sizeof a pointer to the representation. For all the distinct
representations you have to count the sizeof the representation, plus the
sizeof the actual char[] array.

I don't think padding is an issue in your case.

Also, do you want CalcSizeOf(node* pNode) to give the memory consumed by a
node along with its children?

Also, if you make a class with a private member node * pNode, and public
functions to attach/delete child nodes to it and change the values of a node
(such as changing the value of folder), you could keep track of the memory
allocation.

And like hidden bank fees, there could be other hidden costs of memory. So
then we'd have to rely on platform dependent APIs :(.



Casper 08-24-2004 05:48 AM

Re: Using the sizeof operator on a struct?
 
> sizeof(&pNode) == sizeof(node**)
> sizeof(pNode) == sizeof(node*)
> sizeof(*pNode) == sizeof(node)
>
>
> Does that help?
>
> Steve.


What I need is the entire memmory occupied by the pNode incl. its member
data, so sizeof(node) which only yeilds the size of a pointer is not
very interesting. I thought there was a more elegant way, but I've made
a method to calculate it for me. I guess that is the way to go:

int CalcSizeOf(node* pNode)
{
long size = pNode->folder.GetAllocLength() * sizeof(TCHAR);
size += sizeof(pNode->nChildCount);
//Parent pointer AND ourself, hence x2
size += (2 * sizeof(pNode->parent));
return size;
}

Karl Heinz Buchegger 08-24-2004 07:51 AM

Re: Using the sizeof operator on a struct?
 
Casper wrote:
>
> > sizeof(&pNode) == sizeof(node**)
> > sizeof(pNode) == sizeof(node*)
> > sizeof(*pNode) == sizeof(node)
> >
> >
> > Does that help?
> >
> > Steve.

>
> What I need is the entire memmory occupied by the pNode incl. its member
> data, so sizeof(node) which only yeilds the size of a pointer is not
> very interesting. I thought there was a more elegant way, but I've made
> a method to calculate it for me. I guess that is the way to go:
>
> int CalcSizeOf(node* pNode)
> {
> long size = pNode->folder.GetAllocLength() * sizeof(TCHAR);
> size += sizeof(pNode->nChildCount);
> //Parent pointer AND ourself, hence x2
> size += (2 * sizeof(pNode->parent));
> return size;
> }


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

int Size()
{ return sizeof( node ) + folder.GetAllocLength(); }
};

There is no need to sum up the sizes of the immediate member
variables. sizeof applied to a node structure (in contrast
to applying it on a pointer as you did) will do that for you.

--
Karl Heinz Buchegger
kbuchegg@gascad.at


All times are GMT. The time now is 10:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.