C : Call by value or reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Army1987

    #16
    Re: C : Call by value or reference

    On Sun, 15 Jul 2007 17:56:12 +0000, CryptiqueGuy wrote:
    On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
    >>C is a 100% call-by-value language. Function arguments are
    >>expressions ,
    >>not objects. These expressions are evaluated, and those values are
    >>passed to the function.
    This statement is not wrong but highly misleading. Esp. it doesn't
    cover the distinction between the following:
    >>
    void foo1 (stuct something sth);
    void foo2 (stuct something* ptr);
    ^^^^^
    What happened to 'r'? :-)
    >It's all "words". I suspect the OP was looking for pointers - which
    >also makes me wonder if he is a troll. Since how can he implement a
    >garbage collector and not either (a) know the answer or (b) know how to
    >google up the answer.
    >
    Both your declarations follow pass by value. What is there in it?
    Absolutely nothing.
    But if foo2 is called as foo2(&someth) and its definition dereferences
    ptr, foo2 is able to access a variable local to its caller, which foo1
    will never be able to do (unless the struct contains pointers and foo1
    dereferences them.

    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)

    Comment

    • Keith Thompson

      #17
      Re: C : Call by value or reference

      CryptiqueGuy <SRRajesh1989@g mail.comwrites:
      [...]
      Many poorly written C books call "simulation of pass by reference
      using pointers" as pass by reference which confuses a lot of Newbies.
      I feel that Call by reference is said to be supported by a language IF
      it syntactically and semantically supports that facility. C doesn't
      have any syntactical and semantical facility for Call by Reference.
      People find it difficult to understand that simple concept and call
      "simulation of pass by reference using pointers" as REAL pass by
      reference!
      Right. C supports pass-by-reference in the same way that it supports
      linked lists and many other things -- not as a built-in language
      construct, but as something you can construct by programming.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Richard Heathfield

        #18
        Re: C : Call by value or reference

        Roland Pibinger said:
        On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
        >>C is a 100% call-by-value language. Function arguments are
        >>expressions , not objects. These expressions are evaluated, and those
        >>values are passed to the function.
        >
        This statement is not wrong but highly misleading.
        Yes it's not wrong, but no it's not misleading either.
        Esp. it doesn't
        cover the distinction between the following:
        >
        void foo1 (stuct something sth);
        void foo2 (stuct something* ptr);
        But he didn't /ask/ about syntax errors.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Jens Thoms Toerring

          #19
          Re: C : Call by value or reference

          Richard <rgrdev@gmail.c omwrote:
          santosh <santosh.k83@gm ail.comwrites:
          Nehil wrote:
          Does C follow call by value convention or call by reference?
          i see that there is nothing like reference in C standard but it is
          referenced.
          >
          still, what should be the answer for the above question?
          C only supports call by value. However you can simulate call by
          reference using pointers.
          and arrays...
          No, For arrays there's a special rule. Arrays can't be passed
          as a whole by value (this would require a copy of the whole
          array each time the function is called and thus would be rather
          slow) so when an array is found in a context where a value is
          expected (e.g. when an array is used as a function argument
          but also at the right hand side of an assignment). Instead when
          found in such a context ("value context") it is converted auto-
          matically to a pointer to the first element of the array, and
          this value (i.e. the pointer to the first element of the array)
          is what gets passed to the function.

          This isn't really passing the array by reference since within
          the function you have no information anymore about the 'array-
          ness' of the pointer the function received, e.g. you don't
          have any information about the size of the array (unless
          passed to the function as an additional argument) and can't
          distinguish it from a pointer that points to something else
          than an array.

          The article you cite is a rather useless example for what this
          special rule can be (mis-)used - and the accompanying text is
          simply false, there is no "automatic memory allocation" (what-
          ever that is supposed to be) happening at all, there's just an
          one-element array of structures, which is an automatic variable.

          Regards, Jens
          --
          \ Jens Thoms Toerring ___ jt@toerring.de
          \______________ ____________ http://toerring.de

          Comment

          • Keith Thompson

            #20
            Re: C : Call by value or reference

            jt@toerring.de (Jens Thoms Toerring) writes:
            [...]
            No, For arrays there's a special rule. Arrays can't be passed
            as a whole by value (this would require a copy of the whole
            array each time the function is called and thus would be rather
            slow) so when an array is found in a context where a value is
            expected (e.g. when an array is used as a function argument
            but also at the right hand side of an assignment). Instead when
            found in such a context ("value context") it is converted auto-
            matically to a pointer to the first element of the array, and
            this value (i.e. the pointer to the first element of the array)
            is what gets passed to the function.
            [...]

            Just a minor quibble: the idea that arrays are not passed as arguments
            because they're too big is a shaky rationale. Structures (and unions)
            can be passed as arguments (by value, of course), and structures can
            be arbitrarily large. They can even contain arrays.

            Arrays are "second-class" types because the language defines them that
            way; it's a consequence of the way most operations on arrays are
            defined in terms of pointer operations. (But arrays are not pointers,
            and pointers are not arrays; see section 6 of the comp.lang.c FAQ.)

            In the dim past, before the first C standard, structures were also
            second-class types; you couldn't assign structure values, and you
            couldn't pass them as function arguments. Operations on structures
            were done with pointers. (This explains the odd way some of the
            functions in the C standard library are declared; they were first
            implemented before the rules changed.) It was found that the language
            could be changed to allow structure assignment and argument passing
            without breaking anything. The same is not true for arrays. Any
            change allowing arrays to be treated as first-class types would break
            existing code, so we're stuck with the historical baggage.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            • Eric Sosman

              #21
              Re: C : Call by value or reference

              Keith Thompson wrote:
              [...] It was found that the language
              could be changed to allow structure assignment and argument passing
              without breaking anything. The same is not true for arrays. Any
              change allowing arrays to be treated as first-class types would break
              existing code, so we're stuck with the historical baggage.
              Ritchie's "The Development of the C Language" (GIYF)
              gives a pretty clear explanation of his motivation to
              invent the "baggage."

              (As for myself, I think highly of baggage. When the
              airline loses it, I am not pleased.)

              --
              Eric Sosman
              esosman@ieee-dot-org.invalid

              Comment

              • Chris Dollin

                #22
                Re: C : Call by value or reference

                Army1987 wrote:
                On Sun, 15 Jul 2007 17:56:12 +0000, CryptiqueGuy wrote:
                On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
                >>>C is a 100% call-by-value language. Function arguments are
                >>>expression s,
                >>>not objects. These expressions are evaluated, and those values are
                >>>passed to the function.
                >This statement is not wrong but highly misleading. Esp. it doesn't
                >cover the distinction between the following:
                >>>
                >void foo1 (stuct something sth);
                >void foo2 (stuct something* ptr);
                ^^^^^
                What happened to 'r'? :-)
                It's stuct in his keyboad.

                --
                Chis "puns ' us" Dollin

                Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                registered office: Berks RG12 1HN 690597 England

                Comment

                • Chris Dollin

                  #23
                  Re: C : Call by value or reference

                  Richard wrote:
                  Chris Dollin <eh@electriched gehog.netwrites :
                  >
                  >Roland Pibinger wrote:
                  >>
                  >>On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
                  >>>>C is a 100% call-by-value language. Function arguments are expressions,
                  >>>>not objects. These expressions are evaluated, and those values are
                  >>>>passed to the function.
                  >>>
                  >>This statement is not wrong but highly misleading. Esp. it doesn't
                  >>cover the distinction between the following:
                  >>>
                  >>void foo1 (stuct something sth);
                  >>void foo2 (stuct something* ptr);
                  >>
                  >Two functions, distinguished by argument types only. So what?
                  >Structs are passed by value, and pointers are passed by value.
                  >What distinction are you referring to?
                  >
                  Because 99% of the time that C nOObs ask this question they are
                  enquiring how to pass a pointer to a large object as opposed to the
                  entire variable.
                  Oh, I /see/. Hmm. I'll try and bear that in mind the next time I
                  respond to that question -- the usual "what are you actually trying
                  to do?" should get mixed in.

                  --
                  Chris "impressed by the precision of '99%'" Dollin

                  Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                  registered office: Berks RG12 1HN 690597 England

                  Comment

                  Working...