pointer c++//C#

M

mikethebike

Hi
until recently I only knew some VB.NET and there were no pointers. Now
I understand how to use them but still wonder why exactly they are
needed?
What do they look like in C#?
Thanks and cheers
Michael
 
V

Victor Bazarov

mikethebike said:
until recently I only knew some VB.NET and there were no pointers. Now
I understand how to use them but still wonder why exactly they are
needed?

Uh... Because it would be harder without them, maybe?
What do they look like in C#?

Ask in a C# newsgroup or get a book and look it up.

V
 
J

Joshua Maurice

In C# everything is passed by reference by default. In C/C++ not.

Sorry, this is a little pet peeve of mine. Perhaps colloquially, it is
"passed by reference". However, using any sensible technical
definition of the term, C# passes everything by value by default, and
the same for Java, and C. Ex:
void foo(T x) { x = some_value; }
int main()
{ T y;
y = some_value_2;
foo(y);
assert(y == some_value_2);
}
In Java, C, C++, and C#, the assert will pass. This is the very
definition of pass by value. A function which passes by value and
modifies its argument will not have its change affect the caller's
object. Let's take a more Java-specific example:
static void foo(String x) { x = "x"; }
public static void main(String[] args)
{ String y;
y = "y";
foo(y);
assert(y.equals("y"));
}
In this example, changing the argument x inside the function foo does
not change the caller's argument, y. This is because in Java arguments
are passed by value. It's just that the value being passed is a
pointer, what Java annoying calls a "reference". The pointer is being
passed by value, so any manipulation of the pointed-to object will be
visible to the caller, but any manipulation of the passed-by-value
object, the pointer itself, will not be visible to the caller. This is
fundamentally different than pass by reference:
void foo(int & x) { x = 1; }
int main()
{ int y;
y = 2;
assert(y == 2);
foo(y);
assert(y == 1);
}
In this case, both asserts will pass. The function foo when it
modifies its argument, x, that change actually modifies the caller's
value.

This a very anal, but important type. Sometimes I see silly
discussions about Java when people write bad code, believing that Java
has pass by reference semantics, when it does not. It has pass by
value semantics, except that most values are "references", aka
pointers. The same is true for C#.
 
A

Adrian Petrescu

In C# everything is passed by reference by default. In C/C++ not.

Sorry, this is a little pet peeve of mine. Perhaps colloquially, it is
"passed by reference". However, using any sensible technical definition
of the term, C# passes everything by value by default, and the same for
Java, and C. Ex:
void foo(T x) { x = some_value; }
int main()
{ T y;
y = some_value_2;
foo(y);
assert(y == some_value_2);
}
In Java, C, C++, and C#, the assert will pass. This is the very
definition of pass by value. A function which passes by value and
modifies its argument will not have its change affect the caller's
object. Let's take a more Java-specific example:
static void foo(String x) { x = "x"; } public static void
main(String[] args) { String y;
y = "y";
foo(y);
assert(y.equals("y"));
}
In this example, changing the argument x inside the function foo does
not change the caller's argument, y. This is because in Java arguments
are passed by value. It's just that the value being passed is a pointer,
what Java annoying calls a "reference". The pointer is being passed by
value, so any manipulation of the pointed-to object will be visible to
the caller, but any manipulation of the passed-by-value object, the
pointer itself, will not be visible to the caller. This is fundamentally
different than pass by reference:
void foo(int & x) { x = 1; }
int main()
{ int y;
y = 2;
assert(y == 2);
foo(y);
assert(y == 1);
}
In this case, both asserts will pass. The function foo when it modifies
its argument, x, that change actually modifies the caller's value.

This a very anal, but important type. Sometimes I see silly discussions
about Java when people write bad code, believing that Java has pass by
reference semantics, when it does not. It has pass by value semantics,
except that most values are "references", aka pointers. The same is true
for C#.

What you say is only true for primitive types in Java (int, double, char,
etc) and Strings. As soon as you start passing Objects around (i.e,
almost all the time in "real life"), you'll see the "pass by reference"
behavior. You have to go to some lengths (Cloneable interface) to make it
work the way you describe.
 
S

SG

[...]
This a very anal, but important type. Sometimes I see silly discussions
about Java when people write bad code, believing that Java has pass by
reference semantics, when it does not. It has pass by value semantics,
except that most values are "references", aka pointers. The same is true
for C#.

What you say is only true for primitive types in Java (int, double, char,
etc) and Strings.

No, it's not. Every "non-primitive" type In Java is a reference type
and references are passed by value. If you think otherwise chances are
you don't distinguish between references and the objects references
refer to.
As soon as you start passing Objects around

That's the point. You don't "pass objects around". You pass references
around -- by value that is. No variable lets you hold an object
directly (I'm using Java's definition of the term "object" here which
only applies to class objects, arrays and interface objects).

Cheers,
SG
 
A

Alf P. Steinbach

* Adrian Petrescu:
What you say is only true for primitive types in Java (int, double, char,
etc) and Strings. As soon as you start passing Objects around (i.e,
almost all the time in "real life"), you'll see the "pass by reference"
behavior. You have to go to some lengths (Cloneable interface) to make it
work the way you describe.

Sorry, that's incorrect.

Java passes everything by value. There is no type-dependent difference, even
though it might seem so when one is not familiar with what goes on under the
hood. There is no way in Java to have the call

foo( v );

change the value of the variable v, although the object that v references can be
changed.

Thus, experienced Java programmers sometimes simulate pass by reference by
introducing an additional indirection (the universal solution to anything), such
as passing an array -- since the array object can be modified...

On the other hand, C# has pass by reference. C# inherited almost all the Java
semantics but differs in this key respect. So since C# is otherwise so similar
to Java it is perhaps easier to see that Java lacks pass by reference when
comparing it to C#, which does have pass by reference (instead of comparing Java
to the very dissimilar C++, where you have to understand things to compare).


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top