pointer c++//C#

Discussion in 'C++' started by mikethebike, Dec 3, 2009.

  1. mikethebike

    mikethebike Guest

    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
    mikethebike, Dec 3, 2009
    #1
    1. Advertising

  2. mikethebike wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 3, 2009
    #2
    1. Advertising

  3. On Dec 3, 8:26 am, Jensen Somers <> wrote:
    > mikethebike wrote:
    > > 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

    >
    > 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#.
    Joshua Maurice, Dec 3, 2009
    #3
  4. On Thu, 03 Dec 2009 12:28:20 -0800, Joshua Maurice wrote:

    > On Dec 3, 8:26 am, Jensen Somers <> wrote:
    >> mikethebike wrote:
    >> > 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

    >>
    >> 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.
    Adrian Petrescu, Dec 6, 2009
    #4
  5. mikethebike

    SG Guest

    On 6 Dez., 23:04, Adrian Petrescu <> wrote:
    > On Thu, 03 Dec 2009 12:28:20 -0800, Joshua Maurice wrote:
    > > [...]
    > > 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
    SG, Dec 6, 2009
    #5
  6. * Adrian Petrescu:
    > On Thu, 03 Dec 2009 12:28:20 -0800, Joshua Maurice wrote:
    >>
    >> 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.


    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
    Alf P. Steinbach, Dec 7, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    676
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    827
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,234
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    626
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    445
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page