473,230 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

struct or class?

I have an object which is just a thin wrapper over an other object and might
be created in big quantities.
something like that:

// ===== pseudo-code cample =======
class TheObject
{
int[] data;

TheWrapper Data { get { return new TheWrapper(data); } }
}

class or struct TheWrapper
{
int[] data;

public TheWrapper(int[] data)
{
this.data = data;
}
public int Length { get { return data.Length; } }
public int this[int index] { get { return data[index]; } }

public int UtiliyOneLikeSearch() {}
//... etc ...
}
// ========= end of pseudo-code sample ==========
Now it is very likely that aTheObject.Data would be called many times used
for a few lines and forgot.

I thought it might be much more efficient memory wise to make TheWrapper a
struct.

but somehow I feel it's quite unconventional.
what do you think?
Jun 25 '06 #1
9 1301
Implement Idisposable interface and destructor rather.
http://msdn2.microsoft.com/en-us/lib...le(VS.80).aspx

chanmm

"Lloyd Dupont" <net.galador@ld> wrote in message
news:Op**************@TK2MSFTNGP02.phx.gbl...
I have an object which is just a thin wrapper over an other object and
might be created in big quantities.
something like that:

// ===== pseudo-code cample =======
class TheObject
{
int[] data;

TheWrapper Data { get { return new TheWrapper(data); } }
}

class or struct TheWrapper
{
int[] data;

public TheWrapper(int[] data)
{
this.data = data;
}
public int Length { get { return data.Length; } }
public int this[int index] { get { return data[index]; } }

public int UtiliyOneLikeSearch() {}
//... etc ...
}
// ========= end of pseudo-code sample ==========
Now it is very likely that aTheObject.Data would be called many times used
for a few lines and forgot.

I thought it might be much more efficient memory wise to make TheWrapper a
struct.

but somehow I feel it's quite unconventional.
what do you think?

Jun 25 '06 #2
chanmm <ch*****@hotmail.com> wrote:
Implement Idisposable interface and destructor rather.
http://msdn2.microsoft.com/en-us/lib...le(VS.80).aspx


Why? There's no suggestion of anything which indicates that IDisposable
is required, or any need for a finalizer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 25 '06 #3
"Lloyd Dupont" <net.galador@ld> wrote:
I thought it might be much more efficient memory wise to make TheWrapper a
struct.


It might be slightly more efficient, but you won't know until you
measure. Be careful, though: structs have different semantics to
classes, since they get copied around rather than referenced. Structs
work best for immutable values - i.e. the methods and properties on the
struct don't change the internal state of the struct. That way you can
avoid accidentally modifying a copy rather than the original, if
somebody writes code that mistakes a struct for a class.

-- Barry

--
http://barrkel.blogspot.com/
Jun 25 '06 #4
why?
my concern being performance I'm afraid write a finalizer might worsen the
performance, not improve it.

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
"chanmm" <ch*****@hotmail.com> wrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
Implement Idisposable interface and destructor rather.
http://msdn2.microsoft.com/en-us/lib...le(VS.80).aspx

chanmm

"Lloyd Dupont" <net.galador@ld> wrote in message
news:Op**************@TK2MSFTNGP02.phx.gbl...
I have an object which is just a thin wrapper over an other object and
might be created in big quantities.
something like that:

// ===== pseudo-code cample =======
class TheObject
{
int[] data;

TheWrapper Data { get { return new TheWrapper(data); } }
}

class or struct TheWrapper
{
int[] data;

public TheWrapper(int[] data)
{
this.data = data;
}
public int Length { get { return data.Length; } }
public int this[int index] { get { return data[index]; } }

public int UtiliyOneLikeSearch() {}
//... etc ...
}
// ========= end of pseudo-code sample ==========
Now it is very likely that aTheObject.Data would be called many times
used for a few lines and forgot.

I thought it might be much more efficient memory wise to make TheWrapper
a struct.

but somehow I feel it's quite unconventional.
what do you think?


Jun 26 '06 #5
They would be readonly wrapper, so there is no concern.

My main concern is that the underlying object won't be an array but a
reference to a custom type, which would change (not the reference but it's
"content").
hence the wrapper will too (much in the same ways as ReadOnlyLists).
That might be rather unintuitive for a struct.
--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:7k********************************@4ax.com...
"Lloyd Dupont" <net.galador@ld> wrote:
I thought it might be much more efficient memory wise to make TheWrapper
a
struct.


It might be slightly more efficient, but you won't know until you
measure. Be careful, though: structs have different semantics to
classes, since they get copied around rather than referenced. Structs
work best for immutable values - i.e. the methods and properties on the
struct don't change the internal state of the struct. That way you can
avoid accidentally modifying a copy rather than the original, if
somebody writes code that mistakes a struct for a class.

-- Barry

--
http://barrkel.blogspot.com/

Jun 26 '06 #6
On Mon, 26 Jun 2006 10:54:15 +1000, "Lloyd Dupont" <net.galador@ld>
wrote:
They would be readonly wrapper, so there is no concern.

My main concern is that the underlying object won't be an array but a
reference to a custom type, which would change (not the reference but it's
"content").
hence the wrapper will too (much in the same ways as ReadOnlyLists).
That might be rather unintuitive for a struct.


If the wrapper is ReadOnly and will be created many times could it be
worthwhile to cache the wrapper within the object?

If the wrapper copies the data to make it immutable you when the object
is next modified, the object can drop the reference to the current
wrapper and return a new one when next requested. Not even sure if
thread syncronization is a problem here as it wouldn't really matter if
you got two wrappers created.
Jun 26 '06 #7
"Lloyd Dupont" <net.galador@ld> wrote:
They would be readonly wrapper, so there is no concern.

My main concern is that the underlying object won't be an array but a
reference to a custom type, which would change (not the reference but it's
"content").
hence the wrapper will too (much in the same ways as ReadOnlyLists).
That might be rather unintuitive for a struct.


Classes have other benefits beyond structs. For example, you can
eliminate a public constructor taking no arguments, while every struct
has a public constructor taking no arguments, which initializes the
struct to all zeros.

I think you need to measure the performance improvements that may or may
not occur when you toggle between struct and class, and decide if the
performance benefits, if any, are worth the difference in semantics.

-- Barry

--
http://barrkel.blogspot.com/
Jun 26 '06 #8
PS: I don't copy any data.
Just a reference to a class.
And I provide only the read / get / view method to it (in the wrapper), much
in the same way as ReadOnlyList.

While the idea to cache the wrapper is good, I try to minimize the number of
instance variable of my main object, as there would be many instance of it
and I need to it to be as compact as possible.

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
"Chris Chilvers" <ke****@dynafus.com> wrote in message
news:pj********************************@4ax.com...
On Mon, 26 Jun 2006 10:54:15 +1000, "Lloyd Dupont" <net.galador@ld>
wrote:
They would be readonly wrapper, so there is no concern.

My main concern is that the underlying object won't be an array but a
reference to a custom type, which would change (not the reference but it's
"content").
hence the wrapper will too (much in the same ways as ReadOnlyLists).
That might be rather unintuitive for a struct.


If the wrapper is ReadOnly and will be created many times could it be
worthwhile to cache the wrapper within the object?

If the wrapper copies the data to make it immutable you when the object
is next modified, the object can drop the reference to the current
wrapper and return a new one when next requested. Not even sure if
thread syncronization is a problem here as it wouldn't really matter if
you got two wrappers created.

Jun 26 '06 #9
Yes.. I will wait until I'm able to profile the application.
Perhaps it doesn't matter at all!
And yes it was bothering me not to be able to get rid of the parameterless
constructor...

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:1d********************************@4ax.com...
"Lloyd Dupont" <net.galador@ld> wrote:
They would be readonly wrapper, so there is no concern.

My main concern is that the underlying object won't be an array but a
reference to a custom type, which would change (not the reference but
it's
"content").
hence the wrapper will too (much in the same ways as ReadOnlyLists).
That might be rather unintuitive for a struct.


Classes have other benefits beyond structs. For example, you can
eliminate a public constructor taking no arguments, while every struct
has a public constructor taking no arguments, which initializes the
struct to all zeros.

I think you need to measure the performance improvements that may or may
not occur when you toggle between struct and class, and decide if the
performance benefits, if any, are worth the difference in semantics.

-- Barry

--
http://barrkel.blogspot.com/

Jun 26 '06 #10

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

Similar topics

21
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
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
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,...
15
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...
15
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...
3
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
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...
5
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. ...
1
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...
2
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.