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

Home Posts Topics Members FAQ

Struct VS Class

Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequ ential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequ ential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
5 7191
Structs vs. Classes
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.

Article from MSDN

Dincer Uyav
ne*********@wel ovedotnet.net

"Chua Wen Ching" wrote:
Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequ ential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequ ential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2
Hi Dincer Uyav,

Does it mean passing by value will increase more performance over by passing by reference?

Hmm, i thought pass by refernce should be faster right? Coz it only holds the address rather than the actual value.

Correct me if i am wrong. Thanks.
--
Regards,
Chua Wen Ching :)
"Dincer Uyav" wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.

Article from MSDN

Dincer Uyav
ne*********@wel ovedotnet.net

"Chua Wen Ching" wrote:
Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequ ential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequ ential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #3
When you pass by value you already know what the value is and have access to the value. With the pass by refrence you have one level of indirection and need to locate and access that memory address. Depending on the size of the value and refrence there is a point where a reference would be better than by value. Like a large struct and an array of them could degrade performance in this case you might just want to pass by refrence the start of the array of classes.

RAyRAy

"Chua Wen Ching" wrote:
Hi Dincer Uyav,

Does it mean passing by value will increase more performance over by passing by reference?

Hmm, i thought pass by refernce should be faster right? Coz it only holds the address rather than the actual value.

Correct me if i am wrong. Thanks.
--
Regards,
Chua Wen Ching :)
"Dincer Uyav" wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.

Article from MSDN

Dincer Uyav
ne*********@wel ovedotnet.net

"Chua Wen Ching" wrote:
Hi there,

I am very curious on this code:

// declared as structure
[ StructLayout( LayoutKind.Sequ ential )]
public struct Overlapped
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

// declared as class
[ StructLayout( LayoutKind.Sequ ential )]
public class Overlapped2
{
public IntPtr intrnal;
public IntPtr internalHigh;
public int offset;
public int offsetHigh;
public IntPtr hEvent;
}

This is the first time i saw people declaring in Class and later using into platform invocation.

So what is the performance of using class or structures.

As i need to do some low level stuff like smart card, based on my seniors, you need to past structures when you want to write some data into the card. Instead of using structures, can it be done with class too?

Well, i not sure how to explain, but something like this. What are the advantages and disadvantages of using class or structs?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #4
Dincer Uyav <Di********@dis cussions.micros oft.com> wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important
differences that you should be aware of. First of all, classes are
reference types and structs are value types. By using structs, you
can create objects that behave like the built-in types and enjoy
their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on
the heap. However, when you instantiate a struct, it gets created on
the stack. This will yield performance gains. Also, you will not be
dealing with references to an instance of a struct as you would with
classes. You will be working directly with the struct instance.
Because of this, when passing a struct to a method, it's passed by
value instead of as a reference.


Only local variables end up on the stack - if a value type is an
instance or static variable, it will still end up on the heap.

Reference type values are also passed by value - it's just that the
value passed is a reference. There's a big difference between this and
pass-by-reference.

See http://www.pobox.com/~skeet/csharp/parameters.html and
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 16 '05 #5
Thanks RAyRAy and Jon Skeet.

I will look into those urls. Thanks again.
--
Regards,
Chua Wen Ching :)
"Jon Skeet [C# MVP]" wrote:
Dincer Uyav <Di********@dis cussions.micros oft.com> wrote:
Structs vs. Classes
Structs may seem similar to classes, but there are important
differences that you should be aware of. First of all, classes are
reference types and structs are value types. By using structs, you
can create objects that behave like the built-in types and enjoy
their benefits as well.

Heap or Stack?
When you call the New operator on a class, it will be allocated on
the heap. However, when you instantiate a struct, it gets created on
the stack. This will yield performance gains. Also, you will not be
dealing with references to an instance of a struct as you would with
classes. You will be working directly with the struct instance.
Because of this, when passing a struct to a method, it's passed by
value instead of as a reference.


Only local variables end up on the stack - if a value type is an
instance or static variable, it will still end up on the heap.

Reference type values are also passed by value - it's just that the
value passed is a reference. There's a big difference between this and
pass-by-reference.

See http://www.pobox.com/~skeet/csharp/parameters.html and
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 16 '05 #6

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

Similar topics

21
4658
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
11261
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
31580
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being, a Class can have private and protected members, but a Struct everything is Public by default. I laymans terms what would be an appropriate reason to choose a Struct over a Class? So why would one want to choose a Class over a Struct.
15
9084
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
15
3793
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child class. How can I invert these C++ code into pure c code by using struct in C language? Can somebody give me any ideas? thanks. For example, how to conver the following code into pure c code?
3
1919
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
4032
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give it a try here... Okay, here's the deal: I am trying to read from unmanaged memory with a class type struct, this
5
6092
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. #include <fstream> #include <iostream> #include <iomanip> using namespace std; struct pyrll
1
2269
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with a b-spline surfcae. The problem class is the polygon class. this class read 3d files generates faces, edges and so on. here are two of the building blocks(structs) struct FACE{
2
2305
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the pointers have to point to the full node type, and have to be referenced before that full node type is known. One solution is to use the 'fixpoint construction' to get an apparent circular dependency... class c_Final : public c_Layer2< c_Layer1...
0
9705
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
10564
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
10073
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
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
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
6846
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
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.