473,900 Members | 4,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using ref

Hi,

Form a performance perspective, is it wise to use the ref statement as much
as possible?

Thanks!
Arjen

Jul 2 '08
65 3948
J.B. Moreno wrote:
Hilton <no****@nospam. comwrote:
-snip differences between pass by value and pass by reference
argument-
>OK, back to basics and as simple as I can make it. Let use the C#
code:

void Go ()
{
int x = 10;

Method (ref x);
}

void Method (ref int y)
{
y = 20;
}

I am claiming that this is exactly the same as the C code that has
"Method (&x)" (caller) and "Method (int *py)" (callee). All that is
happening is that the compiler is hiding the 'ugliness' from you, so
that when you write "y=20" (above), it really does a "*py= 20".

This is the first time I've ever seen understanding what is happening
on a machine level cause a misunderstandin g....

You are ignoring what ELSE you can do in C, but can't in C# -- namely,
you can assign a different address to py.
so is this "pass-by-reference"?

void Func(int * const py);

Of course not, although py is now permanently set to the original actual
parameter. You can still do things like (py - arrayBase) which a C#
pass-by-reference doesn't permit. But a C++ pass-by-reference does...

void Func2(int& ry);

You can most certainly say (&ry - arrayBase) to get the exact same effect.
In C++, the reference syntax is no more and no less than syntactic sugar.
In C#, the reference syntax is much more. Calling these both by the same
name is more confusing than calling the C code pass-by-reference. There are
two "real concepts" going on:

number of levels of indirection
type-safety
>
void Method (int *py) {
int z;
py=&z;
*py = 2;
}

In C# y can never be anything but another way of saying x.

>So IMHO, it is just some language/compiler smoke and mirrors, the
*same thing* is happening under the covers.

All languages and compilers are just smoke and mirrors over the
hardware. But it's important smoke and mirrors.

Jul 8 '08 #61
You remind me of the people who continue to cling to the idea that
Iraq had something to do with the 9/11 attacks and that Hussein was
hiding weapons of mass destruction somewhere in his country, in spite
of absolutely no evidence in support of those ideas and in spite of
volumes of evidence to the contrary.

In other words, you've decided on your dogma and no amount of actual
facts will have any effect on your beliefs.
Unfortunately for you, Peter, here you paint yourself as the dogma, not
facts, type.

Plenty of "dual-use" explosives and biological agents were found in Iraq.
That they were found in bunkers with military gas masks nearby suggests that
they were in fact part of a WMD program. Not to mention the documented
instances of Hussein actually using WMD on large segments of his population.

Which is why we ought to restrict this group to programming.
Jul 8 '08 #62
The "Furthermor e, const can only be applied to intrinsic types" was (I
believe) a statement about C++ that raylopez99 found by Googling. I
was trying to do a cross-language comparison.
Certainly not true. C++ 'const' can be applied to any variable of any type,
the type pointed to be any pointer or reference, and so on.

C++ 'const' is more like (but much more useful than) C# 'readonly'.
C++0x introduces 'constexpr' which is similar to (but again much more
powerful than) C# 'const'.
Jul 8 '08 #63
>and I barely
>figured out the difference between ref and non-ref (pass by value)
just recently. This thread was good as a refresher. ANd see my
other thread why C# is flawed because you cannot pass an object
using, as in C++, a 'const' keyword to prevent it from being
modified.

It doesn't prevent something from being modified in C++ either.
It does, unless the callee goes out of its way to break its contract (i.e.
uses the const_cast keyword to remove the const). There's no way to turn a
pointer-to-const into a pointer-to-non-const without either a cast or a
union or some really hairy pointer arithmetic, all of which are known to
break type-safety.
>
More importantly, a much better way to ensure that a data structure
isn't modified is simply to make that data structure immutable. For
example, the String class is essentially "const" all the time. Works
great.
Not in C++ it isn't. No matter how immutable you make your object, C++ can
still overwrite the innards if you explicitly work to do so.
>
The lack of "const" for methods or their parameters doesn't make C#
any more flawed than any of the other languages people use on a daily
basis. It just makes it different from one of them.

If having "const" methods and parameters was such an important
feature, everyone would use C++ and no one would use C#, Java, VB,
etc. Odd, then, that so many people find languages other than C++
preferable, or at the very least, just as useful.
Or maybe it is something they really want, but they want garbage collection
and reflection more.

Or, more likely, all the Microsoft C# training never even mentions the
advantages of const-correctness, so they don't even know what they are
missing.
Jul 8 '08 #64
On Tue, 08 Jul 2008 07:42:10 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nos pamwrote:
>It doesn't prevent something from being modified in C++ either.

It does, unless the callee goes out of its way to break its contract
(i.e.
uses the const_cast keyword to remove the const).
What does it matter _what_ the exception is? The fact is, the exception
exists. As long as the exception exists, it's trivial for someone to
modify something declared as "const" in this context.
>More importantly, a much better way to ensure that a data structure
isn't modified is simply to make that data structure immutable. For
example, the String class is essentially "const" all the time. Works
great.

Not in C++ it isn't. No matter how immutable you make your object,
C++ can
still overwrite the innards if you explicitly work to do so.
This isn't a C++ newsgroup. And presumably the OP would just use "const"
if they were using C++. So forgive me for not going out of my way to
qualify my statements explicitly so that everyone reading them knew they
applied to C#, not C++. It never occurred to me that someone would be
confused.
>The lack of "const" for methods or their parameters doesn't make C#
any more flawed than any of the other languages people use on a daily
basis. It just makes it different from one of them.

If having "const" methods and parameters was such an important
feature, everyone would use C++ and no one would use C#, Java, VB,
etc. Odd, then, that so many people find languages other than C++
preferable, or at the very least, just as useful.

Or maybe it is something they really want, but they want garbage
collection
and reflection more.
Like I said, "==>IF<== having 'const' methods and parameters ==>WAS SUCH
AN IMPORTANT FEATURE<==...".

The question isn't whether people find it useful at all. It's whether
it's important enough for someone to described a language without the
feature as "deeply flawed".

It's just stupid to state or imply that "const" is a critical language
feature. It obviously isn't, however useful it might be to some people.

Pete
Jul 8 '08 #65
On Tue, 08 Jul 2008 07:15:59 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nos pamwrote:
Unfortunately for you, Peter, here you paint yourself as the dogma, not
facts, type.
Hardly.
Plenty of "dual-use" explosives and biological agents were found in Iraq.
That they were found in bunkers with military gas masks nearby suggests
that
they were in fact part of a WMD program. Not to mention the documented
instances of Hussein actually using WMD on large segments of his
population.
All completely irrelevant to the claims that had been made regarding an
"imminent threat". Statements like yours are useful for people who want
to engage in diversionary tactics, but not so much when it comes down to
the actual facts.
Which is why we ought to restrict this group to programming.
Yes, you really should have stuck to that advice.
Jul 8 '08 #66

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

Similar topics

5
5731
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
3
2170
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections;
3
2447
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use project classes with 'using' meaning
14
5810
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
8
2419
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL into the bin directory but am not able to progress. Can anyone please guide me to an online tutorial on the subject. Thanks,
0
2211
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase .NET 2.0 DataProvider (iAnywhere.Data.AsaClient.dll) into the GAC so it can be used in the ProviderName property of a SQLDataSource and loads properly at run time. The application I'm writing is a bit more complex than the example I'm about to...
10
1981
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL, CN) Dim DR As OleDbDataReader = CMD.ExecuteReader()
0
2585
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation link (1,2,3 so on). The code can be found in SubscriptionCart.aspx.cs. Default.aspx ------------
3
8320
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
6
5180
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick application working. However, when attempting to implement the solution, the AJAX calls weren't updating the screen like the examples were and seemed not to fire until after the long running process had completed. I found the only real...
0
9845
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
10866
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
10978
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
9672
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
5892
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
6084
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4724
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
4305
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
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.