473,804 Members | 3,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class or Struct, and archive to binary data

This is a two parte
1. What to the performance differnace between a Class and a struct. I have a handler class which has a list containing a lot of instances of a class which purely holds data, All the operations are caried out within the handler, would I be better to use a strut instead

2. I want to extact the data from the data holder class / struct, using the hander class archive the data away into a byte steam to either place in a binaryvar in a database, or to send to another application which understands the packing of the data, How do i place all the data in the byte stream (C++ memcpy)

Thanks in advance.
Nov 15 '05 #1
3 1492
The difference is that class is reference type (nearly same as pointer), so
you have list of pointers that point to data which resides in managed heap.

If you will uses structs than you will also have pointers but, they will
point to the stack memory. If you have large number of objects it is better
to keep them in managed heap that is they must be classes.

you can use BinaryFormatter .Serialize(...) or, you can use BinaryWriter to
write in the custom way to the stream.

"Moger" <an*******@disc ussions.microso ft.com> wrote in message
news:A0******** *************** ***********@mic rosoft.com...
This is a two parter
1. What to the performance differnace between a Class and a struct. I have a handler class which has a list containing a lot of instances of a
class which purely holds data, All the operations are caried out within the
handler, would I be better to use a strut instead?
2. I want to extact the data from the data holder class / struct, using the hander class archive the data away into a byte steam to either place in
a binaryvar in a database, or to send to another application which
understands the packing of the data, How do i place all the data in the byte
stream (C++ memcpy).
Thanks in advance.

Nov 15 '05 #2
Vadym Stetsyak <pd****@ukr.net > wrote:
The difference is that class is reference type (nearly same as pointer), so
you have list of pointers that point to data which resides in managed heap.

If you will uses structs than you will also have pointers but, they will
point to the stack memory. If you have large number of objects it is better
to keep them in managed heap that is they must be classes.


No they won't. If you have a list of value types (eg an array or an
ArrayList), those will be on the heap. Structs aren't always stored on
the stack, contrary to popular myth.

See http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
"Vadym Stetsyak" <pd****@ukr.net > wrote in message
news:uX******** ******@TK2MSFTN GP11.phx.gbl...
The difference is that class is reference type (nearly same as pointer), so you have list of pointers that point to data which resides in managed heap.
If you will uses structs than you will also have pointers but, they will
point to the stack memory. If you have large number of objects it is better to keep them in managed heap that is they must be classes.


A struct is a value type, which does not have a pointer. The actual value
of a value type is copied between variables.

The description of a value type being on the stack is a bit misleading
because it suggests an absolute condition that isn't true. A value type is
copied to the stack if it is a parameter or local variable. However, if it
is a member of another type, it is stored where its enclosing type is
stored. For an enclosing reference type, it will reside in-line with the
reference type on the heap. For an enclosing value type it will reside
wherever the enclosing value type resides, which could be either the stack,
or in-line on the heap. Here's an article that describes this in more
detail:

http://www.yoda.arachsys.com/csharp/memory.html

Joe
--
http://www.csharp-station.com
Nov 15 '05 #4

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

Similar topics

2
2065
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago I was suggested by the list to use the struct module to parse it using the following code in the hypothesis that for each records I have just two fields: import struct fmt='10s1960s' size=struct.calcsize(fmt)
5
5459
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are coming through fine when using (integer1,) = struct.unpack('l', line) except when line contains "carriage-return" "linefeed" which are valid binary packed values. Error = unpack string size dows not match format. It seems that
6
23609
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
12
1770
by: bartek | last post by:
template <class T, unsigned size> struct Array { typedef unsigned SizeType; typedef T ValueType; typedef T* Ptr; typedef T const* ConstPtr; // Default copy ctor and assignment op are fine. SizeType Size() const { return size; }
7
1914
by: Andrew Robert | last post by:
Hi Everyone, I am having a problem with a class and hope you can help. When I try to use the class listed below, I get the statement that self is not defined. test=TriggerMessage(data) var = test.decode(self.qname)
10
2281
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it for non-trivial tasks, I always happen to write small wrapper functions to adjust the values returned by struct. An example API would be the following: ============================================
4
4413
by: Daniel Mark | last post by:
Hello all: I have found a useful module in IPython, named 'from IPython.ipstruct import Struct". So I can use it as follows: #################################### from IPython.ipstruct import Struct
7
1520
by: Pep | last post by:
I'm getting weird results at the moment so thought I'd rebase my knowledge of c++ storage with your help :) I have a class used as a type in a struct and the struct is handled by a 3rd party binary writed for persistent storage. I am confused by the results I am getting and am not sure how the 3rd party writer is seeing the size of the stuct. class foo // I have not included the methods for brevity
0
943
by: witichis | last post by:
Hi, I wrote a class to read in a binary file: see the code of readWLdata.py below --------------8<------------------------------- test.py from readWLdata import block if __name__ == "__main__": print "read WL data"
0
9707
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
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6856
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
5525
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
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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

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.