473,402 Members | 2,046 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,402 software developers and data experts.

Passing a (Generic) Stack by Reference

Hi all,

I'm working on a C# project, and at some point I'm needing to pass the
same stack to multiple functions.

I found out the hard way that when I poss a stack by value it doesn't
actually use a copy constructor to move the contents over to a new
stack - the original stack is still modified by changes to the stack
that was passed by value.

Looking at the MSDN page (http://msdn2.microsoft.com/en-us/library/
3278tedw.aspx) I don't see anything that explicitly says it's (un)safe
to pass by reference.

My question: is there an easy way to copy this stack to a new one
without creating my own "stack copy" function?
Also: In the future, how can I tell if it is safe to pass *and modify*
a stack to a new function?

Thanks,

Aug 27 '07 #1
4 2565
Mahmoud Al-Qudsi <mq****@gmail.comwrote:
I'm working on a C# project, and at some point I'm needing to pass the
same stack to multiple functions.

I found out the hard way that when I poss a stack by value it doesn't
actually use a copy constructor to move the contents over to a new
stack - the original stack is still modified by changes to the stack
that was passed by value.
That's because it's a reference type.

See http://pobox.com/~skeet/csharp/parameters.html
Looking at the MSDN page (http://msdn2.microsoft.com/en-us/library/
3278tedw.aspx) I don't see anything that explicitly says it's (un)safe
to pass by reference.
You need to be clear about the difference between passing a reference
by value and passing a value by reference.
My question: is there an easy way to copy this stack to a new one
without creating my own "stack copy" function?
new Stack<WhateverType(oldStack) should do it.
Also: In the future, how can I tell if it is safe to pass *and modify*
a stack to a new function?
What kind of "safety" are you concerned about?

--
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
Aug 27 '07 #2
On Aug 27, 12:50 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Mahmoud Al-Qudsi <mqu...@gmail.comwrote:
I'm working on a C# project, and at some point I'm needing to pass the
same stack to multiple functions.
I found out the hard way that when I poss a stack by value it doesn't
actually use a copy constructor to move the contents over to a new
stack - the original stack is still modified by changes to the stack
that was passed by value.

That's because it's a reference type.

Seehttp://pobox.com/~skeet/csharp/parameters.html
Looking at the MSDN page (http://msdn2.microsoft.com/en-us/library/
3278tedw.aspx) I don't see anything that explicitly says it's (un)safe
to pass by reference.

You need to be clear about the difference between passing a reference
by value and passing a value by reference.
My question: is there an easy way to copy this stack to a new one
without creating my own "stack copy" function?

new Stack<WhateverType(oldStack) should do it.
Also: In the future, how can I tell if it is safe to pass *and modify*
a stack to a new function?

What kind of "safety" are you concerned about?

--
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
Thanks for your reply.

I'm clear on what (and how) it is happening (thanks to painful years
of C++ coding), just not sure how to tell beforehand if it would
happen or not.
Casting the old stack to an IEnumerable and putting that in the new
stack's constructor like you suggested has done the trick - thanks.

I'm already using a thread-safe wrapper for thread-safety, the only
other concern I had was the preservation of data when sending it to
another function - and now that is working fine too.

Thanks :)

Aug 27 '07 #3
It should be noted that the copy of the stack that you make is a shallow
copy. If the elements in the stack are reference types, and you manipulate
elements in the new stack, then those changes are going to be reflected in
the original stack.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mahmoud Al-Qudsi" <mq****@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 27, 12:50 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
>Mahmoud Al-Qudsi <mqu...@gmail.comwrote:
I'm working on a C# project, and at some point I'm needing to pass the
same stack to multiple functions.
I found out the hard way that when I poss a stack by value it doesn't
actually use a copy constructor to move the contents over to a new
stack - the original stack is still modified by changes to the stack
that was passed by value.

That's because it's a reference type.

Seehttp://pobox.com/~skeet/csharp/parameters.html
Looking at the MSDN page (http://msdn2.microsoft.com/en-us/library/
3278tedw.aspx) I don't see anything that explicitly says it's (un)safe
to pass by reference.

You need to be clear about the difference between passing a reference
by value and passing a value by reference.
My question: is there an easy way to copy this stack to a new one
without creating my own "stack copy" function?

new Stack<WhateverType(oldStack) should do it.
Also: In the future, how can I tell if it is safe to pass *and modify*
a stack to a new function?

What kind of "safety" are you concerned about?

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

Thanks for your reply.

I'm clear on what (and how) it is happening (thanks to painful years
of C++ coding), just not sure how to tell beforehand if it would
happen or not.
Casting the old stack to an IEnumerable and putting that in the new
stack's constructor like you suggested has done the trick - thanks.

I'm already using a thread-safe wrapper for thread-safety, the only
other concern I had was the preservation of data when sending it to
another function - and now that is working fine too.

Thanks :)

Aug 27 '07 #4
Mahmoud Al-Qudsi <mq****@gmail.comwrote:
I'm clear on what (and how) it is happening (thanks to painful years
of C++ coding), just not sure how to tell beforehand if it would
happen or not.
You just need to know whether you're dealing with a reference type (a
class/delegate/array/interface) or a value type (a struct/enum).
--
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
Aug 27 '07 #5

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

Similar topics

15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
3
by: Scott M. | last post by:
If I pass a reference type ByVal, am I making a copy of the object on the heap or am I making a copy of a pointer to the object on the heap? If I pass a string object (reference type) into a sub...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
8
by: Cool Guy | last post by:
From <http://msdn2.microsoft.com/en-us/library/f4a6ta2h(en-US,VS.80).aspx>: | As with the previous use of the Stack<T> class created with the Order | type, another instance of the specialized...
6
by: osama178 | last post by:
Let's say I have this code -------------------- class GenericQueue { public: bool Pop(void*& refToPtr); //--------------(1) };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...

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.