Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using the sizeof operator on a struct?

Reply
Thread Tools

Using the sizeof operator on a struct?

 
 
Casper
Guest
Posts: n/a
 
      08-24-2004
Thanks for the clarification!
Casper

 
Reply With Quote
 
 
 
 
Casper
Guest
Posts: n/a
 
      08-24-2004
> Your function calculates a long, but returns an int! Beware losing
> precision.

Yes thanks, that is a clearly a mistake!

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

That's the overall goal, right now I am summing up while building my tree.

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

Yes this is the true OO way, however in my attempt to minimize memmory
use I do not keep an eye on children at all, but let a node know its
parent and a counter to increment/decrement when I add delete childs.

This way my cleaning up algorithms are a bit complicated but I appear to
save 30% on each node - again with 50.000+ nodes mem use is a critical
requirement for me.

Thanks for your feedback!
Casper

 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      08-25-2004
"Casper" <(E-Mail Removed)> wrote in message news:kWJWc.110624

> Yes this is the true OO way, however in my attempt to minimize memmory
> use I do not keep an eye on children at all, but let a node know its
> parent and a counter to increment/decrement when I add delete childs.


How is the counter used?

You could always do something like this:

struct Whatever {
string folder;
etc;
#if defined(_DEBUG)
std::list<Whatever *> children;
#endif
};

> This way my cleaning up algorithms are a bit complicated but I appear to
> save 30% on each node - again with 50.000+ nodes mem use is a critical
> requirement for me.


Please explain further. How do you manage to save memory?


 
Reply With Quote
 
Casper
Guest
Posts: n/a
 
      08-26-2004
Well if I wanted to be able to clean from memmory, all nodes in a tree,
I would have to let the root node know its childs, these childs know
their childs etc. so a simple DFS destructor calling could clean up.

However, this would require an array, linket list or whatever to point
to these childs of every node. Eg. "C:\" would have to point to "Program
files", "WinNT" etc.

However, since I am building a directory tree primarily to allow file
structures (which I keep in an independent array) with members pointing
to a node in my directory tree, all I really need is to make sure I:

- Do not add directory nodes to me tree if no files will reference it.
(Since then, I would have no way of deleting it!)
- When the last file or directory of a node is deleted, the node itself
must be deleted as well with its recursive empty parents.

This bottum-up approach is a bit hard to explain, it works very well
however - and as I mentioned, a node can remain completely ignorant of
its childs as long as it knows its parent node.


// Cleanup rutine
for(int n=0;n < pFiles.GetCount(); n++)
{
pFile = (file*)pFiles.ElementAt(n);
// Remember folder so we can iterate "up" and clean
node* folder = pFile->dir;
// Delete the file entry
delete pFile;
// If the pFile was the only pointing to node
while(folder->parent != NULL && folder->nChildCount == 1)
{
// Then we can delete the node as it is unreferenced
node* folderCopy = folder;
folder = folderCopy->parent;
delete folderCopy;
}
folder->nChildCount--;
}

/Casper


Siemel Naran wrote:
> "Casper" <(E-Mail Removed)> wrote in message news:kWJWc.110624
>
>
>>Yes this is the true OO way, however in my attempt to minimize memmory
>>use I do not keep an eye on children at all, but let a node know its
>>parent and a counter to increment/decrement when I add delete childs.

>
>
> How is the counter used?
>
> You could always do something like this:
>
> struct Whatever {
> string folder;
> etc;
> #if defined(_DEBUG)
> std::list<Whatever *> children;
> #endif
> };
>
>
>>This way my cleaning up algorithms are a bit complicated but I appear to
>>save 30% on each node - again with 50.000+ nodes mem use is a critical
>>requirement for me.

>
>
> Please explain further. How do you manage to save memory?
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The sizeof operator : sizeof(++i) Kislay C Programming 10 10-19-2007 09:28 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments