473,803 Members | 4,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design question : struct or class

Hello,

I've got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there's no real business logic my choice would be structs. But looking
at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves
Nov 15 '05
26 1941
L#
On Sun, 1 Feb 2004 11:26:10 -0700, "Joe Mayo [C# MVP]"
<jm***@nospamAt CSharpDashStati on.com> wrote:

application. I've never seen or heard anything that would make me believe
that the size of a value type determines whether they reside on the stack or
on the heap.


Mmm, indeed, I thought I read somewhere that the framework checks
this, but apparently it doesn't, because using large structs has a
negative effect in perfomance.

[Source:
http://msdn.microsoft.com/library/de...assStruct.asp]

We learn, every day.

--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #21
Enjoyed the article - thanks for pointing it out :) Peter
"L#" <ludwig_(nospam please)stuyck@p andora(nospampl ease).be> wrote in message
news:0f******** *************** *********@4ax.c om...
On Sun, 1 Feb 2004 11:26:10 -0700, "Joe Mayo [C# MVP]"
<jm***@nospamAt CSharpDashStati on.com> wrote:

application. I've never seen or heard anything that would make me believethat the size of a value type determines whether they reside on the stack oron the heap.
Mmm, indeed, I thought I read somewhere that the framework checks
this, but apparently it doesn't, because using large structs has a
negative effect in perfomance.

[Source:

http://msdn.microsoft.com/library/de...assStruct.asp]
We learn, every day.

--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be

Nov 15 '05 #22
<L# <ludwig_(nospam please)stuyck@p andora(nospampl ease).be>> wrote:
No they are not. "Pass by reference" has a very specific meaning, and
it *doesn't* apply here (unless you use the ref modifier). There is a
big difference between a reference being passed by value (which is what
actually happens) and a parameter itself being passed *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
Being objects, they are passed by value; meaning that the pointer to
the object is passed by value.


Passing an object doesn't happen in .NET. Only passing a reference
happens - and as you say, it's passed by value by default in C#.
Eventually it has the same effect as
being passed by reference.
No it doesn't.
Placing the ref keyword in front of it, won't make a difference.


Yes it will. Here's a sample program:

using System;

public class Test
{
static void Main()
{
string x = "hello";
PassByValue (x);
Console.WriteLi ne (x);
PassByRef (ref x);
Console.WriteLi ne (x);
}

static void PassByValue (string y)
{
y = "there";
}

static void PassByRef (ref string y)
{
y = "there";
}
}

PassByValue and PassByRef are identical apart from the way in which
their parameters are passed - yet they have completely different
effects.

--
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 #23
L#
On Mon, 2 Feb 2004 08:06:35 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:

Placing the ref keyword in front of it, won't make a difference.


Yes it will. Here's a sample program:


But doesn't String behave completely different than other Objects?
--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #24
<L# <ludwig_(nospam please)stuyck@p andora(nospampl ease).be>> wrote:
Placing the ref keyword in front of it, won't make a difference.


Yes it will. Here's a sample program:


But doesn't String behave completely different than other Objects?


No. What makes you say it does?

If you want an example which doesn't use strings at all, here it is:

using System;

public class Test
{
int number;

public int Number
{
get { return number; }
}

Test (int number)
{
this.number = number;
}

static void Main()
{
Test t = new Test(10);
PassByValue(t);
Console.WriteLi ne (t.Number);
PassByRef(ref t);
Console.WriteLi ne (t.Number);
}

static void PassByValue (Test t)
{
t = new Test(20);
}

static void PassByRef (ref Test t)
{
t = new Test(20);
}
}

--
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 #25
L#
On Mon, 2 Feb 2004 11:21:25 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:

No. What makes you say it does?

If you want an example which doesn't use strings at all, here it is:


So if you modify the object, there's no difference in passing by
value/passing by reference; but if you create a new object, there's a
difference. Didn't know that!
--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #26
<L# <ludwig_(nospam please)stuyck@p andora(nospampl ease).be>> wrote:
So if you modify the object, there's no difference in passing by
value/passing by reference; but if you create a new object, there's a
difference. Didn't know that!


The important difference is that modifying the object a parameter's
value refers to doesn't actually change the value of the parameter.
Changing the value of the parameter (e.g. by creating a new object, but
that's only one type of new value) is the important thing, and that's
where the difference between pass-by-reference and pass-by-value
semantics lies.

--
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 #27

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

Similar topics

8
3774
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various events. The callbacks specified by the subscriber will be called when the events get trigerred in a different thread. Each event has different kinds of data associated with it. To achieve this I have the following: // The following describes the...
11
1428
by: | last post by:
I have a rather newbie question regarding design of class hierarchy. Suppose I have a class Class0, and need to implement a public Class0.compute() interface. There are three different ways to choose the implementation, and the user of the class has to be able to make that choice. Here is what I have in mind. I declare three classes, ClassA, ClassB, ClassC, and each of them has a distinct compute() method that can be used by Class0. ...
31
2000
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from food, thus; food / \ / \ meat veg
0
1122
by: ma740988 | last post by:
I've provided a stripped down version - as best I could - to get us by. That said, I'm in a quandry with respect to a design here. Consider: // stream.h #ifndef STREAM_H #define STREAM_H # pragma warning (disable : 4786) // if using microsofts compiler ver6
0
1546
by: ma740988 | last post by:
I'm going through modern C++ design looking for tips and while hi-tech I suspect one solution to my issue would involve the factory design pattern. // algorithms.h class Algorithms { protected: typedef std::deque<double> DDEQUE; // need to make this even more generic to support floats .. i.e float and double public:
5
1849
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The idea is to have a Class that stores elements of Class2 in different ways (arrays in stack memory, arrays in heap memory, using the std::vector and so on). I would like the user to customize the creation of a class with Class2 and StoragePolicy like...
3
2490
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
8
1874
by: obrianpatrick | last post by:
Hi, I am relatively new to object oriented programming and design. I am developing an application in VS 2005. I am having the following design problem: I have two interfaces X and Y. Y is derived from X as the following: __interface X {
1
1615
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { public class Program
0
9703
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
9564
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,...
0
10548
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
10316
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...
0
10069
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
9125
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...
0
6842
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
5500
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...
1
4275
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.