473,807 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating size in bytes of the contents a datastructure

Hi,

I have created my own data structure. Basically it is a two way
'stack', where I can push elements and pop elements both from top and
bottom. I also included a counter to show the number of elements
currently on the data structure.

I need to calculate the size in bytes of this datastructure content at
a certain time. What I am pushing on this datastructure is the same
class instances but with different values.

How can I achieve this.
Thanks in Advance

Mar 1 '06 #1
7 2550
Hi,

That's a tricky question, the first doubt I get is why you need to know
this?

First you need to know especify if you want the sum of the structure and all
the instances or just the structure.
The size of the structure depend of the way you store the collection, what
you use? an array, an ArrayList or something else?

the same thing apply to the instances you are storing, you may want to know
the memory footprint of that instance with/without the inclusion of the
referenced inside it, think, what would happen if the store instance has a
string member, the size of this strnig will change from instance to
instance.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Curious" <th****@mail.gl obal.net.mt> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
Hi,

I have created my own data structure. Basically it is a two way
'stack', where I can push elements and pop elements both from top and
bottom. I also included a counter to show the number of elements
currently on the data structure.

I need to calculate the size in bytes of this datastructure content at
a certain time. What I am pushing on this datastructure is the same
class instances but with different values.

How can I achieve this.
Thanks in Advance

Mar 1 '06 #2
What is required is the sum of the structure and all the instances
inside it.

The code I am using is the following:

I will have an instance of the Stack class given below with data inside
it.
internal class Node
{
# region Private Fields

private object data;
private Node previous;
private Node next;

# endregion
# region Constructors

/// <summary>
/// Creates an instance of the Node class.
/// </summary>
/// <param name="obj">The value to be placed on stack</param>
public Node(object obj)
{
Data = obj;
Previous = null;
Next = null;
} // end constructor

# endregion
# region Data

/// <summary>
/// The value to be placed on stack.
/// </summary>
public object Data
{
set
{
data = value;
} // end set
get
{
return data;
} // end get
} // end property Data

/// <summary>
/// The node previous to this node.
/// </summary>
public Node Previous
{
set
{
previous = value;
} // end set
get
{
return previous;
} // end get
} // end property Previous

/// <summary>
/// The Node next to this node.
/// </summary>
public Node Next
{
set
{
next = value;
} // end set
get
{
return next;
} // end get
} // end property Next

# endregion

} // end class Node
// *************** *************** *********
public class Stack
{
# region Private Fields

private int count;
private Node bottomStack;
private Node topStack;

# endregion

# region Constructors

/// <summary>
/// Creates an instance of the Stack class.
/// </summary>
public Stack()
{
count = 0;
bottomStack = null;
topStack = null;
} // end constructor

# endregion
# region Public Methods

/// <summary>
/// Inserts an object at the top of the stack.
/// </summary>
/// <param name="obj"></param>
public void Push(object obj)
{
Node n = new Node(obj);

if (bottomStack == null)
{
bottomStack = n;
topStack = n;
} // end if
else
{
topStack.Next = n;
n.Previous = topStack;
topStack = topStack.Next;
} // end else

Count++;
} // end method Push

/// <summary>
/// Returns the object that is found on top of the stack.
/// </summary>
/// <returns></returns>
public object PeekTop()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperatio nException("Sta ck empty.");
} // end if
else
{
obj = topStack.Data;
} // end else

return obj;
} // end method PeekTop

/// <summary>
/// Removes and returns the object that is found on top of the stack.
/// </summary>
/// <returns></returns>
public object PopTop()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperatio nException("Sta ck empty.");
} // end if
else
{
obj = topStack.Data;
if (Count == 1)
{
topStack = null;
bottomStack = null;
} // end if
else
{
topStack = topStack.Previo us;
topStack.Next = null;
} // end else if

Count--;
} // end else

return obj;
} // end method PopTop

/// <summary>
/// Returns the object that is found on the bottom of the stack.
/// </summary>
/// <returns></returns>
public object PeekBottom()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperatio nException("Sta ck empty.");
} // end if
else
{
obj = bottomStack.Dat a;
} // end else

return obj;
} // end method PeekBottom

/// <summary>
/// Removes and returns the object that is found on the bottom of the
stack.
/// </summary>
/// <returns></returns>
public object PopBottom()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperatio nException("Sta ck empty.");
} // end if
else
{
obj = bottomStack.Dat a;
if (Count == 1)
{
topStack = null;
bottomStack = null;
} // end if
else
{
bottomStack = bottomStack.Nex t;
bottomStack.Pre vious = null;
} // end else

Count--;
} // end else

return obj;
} // end method PopBottom

/// <summary>
/// Removes all objects from the stack.
/// </summary>
public void Clear()
{
bottomStack = null;
topStack = null;
Count = 0;
} // end method Clear

# endregion
# region Properties

/// <summary>
/// Holds the current size of the Stack
/// </summary>
public int Count
{
set
{
count = value;
} // end set
get
{
return count;
} // end get

} // end property Count

# endregion

} // end class Stack

Mar 1 '06 #3
You could also just use LinkedList<T> to create a double-ended queue and
push or pop elements off either end. As for size, you could use
BinarySerialize r on each element, but that would be pretty slow. Why you
need the binary size of each element?

--
William Stacey [MVP]

"Curious" <th****@mail.gl obal.net.mt> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
| Hi,
|
| I have created my own data structure. Basically it is a two way
| 'stack', where I can push elements and pop elements both from top and
| bottom. I also included a counter to show the number of elements
| currently on the data structure.
|
| I need to calculate the size in bytes of this datastructure content at
| a certain time. What I am pushing on this datastructure is the same
| class instances but with different values.
|
| How can I achieve this.
| Thanks in Advance
|
Mar 1 '06 #4
Regarding the LinkedList datastructure, in which namespace is this
available is this available?
(Currently using VS2003, .Net 1.1)

Basically I have two algorithms:
1) one that simply pushes elements on the data strcutre and pops
nothing
2) the other pushes elements on the data structure and pops old
elements

Now I need to monitor this performance issue against execution time to
show which is most appropriate in certain sitations. If there is
anything else more suitable than binary size, then its not going to
make me a problem.

Any further suggestions
Thanks

Mar 1 '06 #5
| Regarding the LinkedList datastructure, in which namespace is this
| available is this available?
| (Currently using VS2003, .Net 1.1)

..Net 2.0. A version may also be in PowerCollection s. Not sure.
| Now I need to monitor this performance issue against execution time to
| show which is most appropriate in certain sitations. If there is
| anything else more suitable than binary size, then its not going to
| make me a problem.

I am still not understanding. Why you need the size the objects?
Mar 1 '06 #6
HI,
"Curious" <th****@mail.gl obal.net.mt> wrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
Regarding the LinkedList datastructure, in which namespace is this
available is this available?
I don;t find it in 1.1 , maybe 2.0 ?
(Currently using VS2003, .Net 1.1)

Basically I have two algorithms:
1) one that simply pushes elements on the data strcutre and pops
nothing
2) the other pushes elements on the data structure and pops old
elements So you have only one element? what about the first one?

If you talk about push/pop I think in either a Stack of a Queue (in the
latter is called Enqeue/Dequeue )
Now I need to monitor this performance issue what performance issue? this is the first time you mention it
against execution time to
show which is most appropriate in certain sitations. If there is
anything else more suitable than binary size, then its not going to
make me a problem.


What are you trying to do in the first place
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 1 '06 #7
As mentioned I will have 2 algorithms. Now I will have different input
data varying in size.

This data is to be applied to both algorithms. The algorithms have
similar behaviour, where they will use the input data to do some
execution, upon that input just executed some information is generated
and needs to be stored on the datastructure. The main reason to store
this information is that while the algorithms are executing, there may
rise situations to rollback and so pop information from the head of the
data structure till algorithm comes back to a state that is correct.

Now, there will be a time that some information stored on
datastructure(f ound on the bottom) has become old and there will surely
be no need to use again that data to rollback.

Now one of the algorithms is to keep all the information required to
rollback, while the other when some rollback data is not required
anymore is to be removed.

I need to monitor the two algorithms to compare and contrast
performance issues.

I hope this is more clear.

Mar 2 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
5203
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to calculate the totals in each row. when i try however, i receive the error: "Error: 'elements' is null or not an object"
2
22834
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F: operator delete(void*) (vg_replace_malloc.c:156) ==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned) (new_allocator.h:86) ==11604== by 0x8049C08: std::_Vector_base<Foo, std::allocator<Foo> >::_M_deallocate(Foo*,...
0
1690
by: Amit | last post by:
Hi: I'm trying to manually compute the ISIZE in REORGCHK for indexes in DB2 v8 (fp 4). Based on the documentation in Administrative Guide for v8, for non lob fields ISIZE should be the aggregate of avg datasize of the index columns + 2 bytes (1 byte for v7) overhead for each varchar and vargraphic column and 1 byte overhead for each null column. I use AVGCOLLEN in syscat.columns for the column size, and add 2 bytes
2
2537
by: kuhni | last post by:
Hi everybody! After searching newsgroups for a couple of hours, I now try asking directly (running the risk of asking again the same question). My problem is to predict when the size of the database (1GB I expect) is over 1GB by calculating the maximal number of data sets (tuple) added: 1. Is the following calculation for estimating the mdb file size in Access 97 correct: 1 integer field = 4 Byte
5
11954
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int SizeOfArray(int a) { return (sizeof(a)/sizeof(a)); } main() { int a={1,2,3}; printf("%d\n",SizeOfArray(a));
31
3750
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the malloc package to find out how big an allocated block is". ( Question 7.27) Is there somwhere explained why - because it would seem to me, that free()
49
61197
by: Sam | last post by:
Hi all, Is there a function in the standard library that can get the size of a file? Thank you very much. Sam.
12
17754
by: PiotrKolodziej | last post by:
Hi I have long variable containing number of stored bytes. I want to display Bytes , KB, MB, GB depending of the size of this variable as a string. Does framework provide any class for such a fast calculation or i have to divide by 1024, check if result is zero, if not divide again and so on...? Thanks PK
4
2091
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Say I have a class like, class Sample { public decimal first = 10; public decimal second = 20; } I have initialized it
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.