About calling by reference in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ThuKhanh
    New Member
    • Apr 2007
    • 7

    About calling by reference in C

    So far I just use the call-by-reference method but don't pay attention to that.
    But now there is a question : in this method , we pass pointers to the fuction , so, after the function call , the pointers will be thrown away (because all local variables in a function , including the argument passed to it , are stored in a stack during its execution and freed when finished) . So that will affect our variables used to pass ?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by ThuKhanh
    So far I just use the call-by-reference method but don't pay attention to that.
    But now there is a question : in this method , we pass pointers to the fuction , so, after the function call , the pointers will be thrown away (because all local variables in a function , including the argument passed to it , are stored in a stack during its execution and freed when finished) . So that will affect our variables used to pass ?
    C uses the call by value mechanism only; there is no call by reference mechanism
    implemented in C; that's all there is to it.Even pointers are passed by value, i.e.
    the value of the pointer is copied into the actual parameter; no more no less.

    kind regards,

    Jos

    Comment

    • ThuKhanh
      New Member
      • Apr 2007
      • 7

      #3
      the pointer is passed by value,yes, but there is the problem when we work with pointers: once two pointers have pointed to the same mem zone(for example when assignn p=q) when we throw away one of them , use of the other will create an error ( program works with variable just memory addr , doesn't it ?)
      And this case is similar ! Pass by reference is only pass by value with the value is a pointer , i agree , but pointers are related .

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by ThuKhanh
        the pointer is passed by value,yes, but there is the problem when we work with pointers: once two pointers have pointed to the same mem zone(for example when assignn p=q) when we throw away one of them , use of the other will create an error ( program works with variable just memory addr , doesn't it ?)
        And this case is similar ! Pass by reference is only pass by value with the value is a pointer , i agree , but pointers are related .
        I don't know if I understand your question, but yes indeed if two pointers point to
        the same memory region (p= q) and you free() the memory, you can't dereference
        your pointers anymore. Passing the values of pointers around by value still
        hasn't got anything to do with the pass by reference parameter passing mechanism.

        kind regards,

        Jos

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You question points out one of the worst problems in using pointers: You cannot delete memory unles you know your pointer is the ONLY ONE in the program that points to that memory.

          If you don't know that, you cannot reliably free memory at all without running the risk of crashing somewhere else by trying to free the memory again.

          That means you will need to count the copies of your pointers. When you call a function you increment the count. When the called function is done with the pointer, it decrements the count. Doing this manually is not reliable. Using C, you have to do this manually so passing deletable pointers around a C program is very dangerous.

          C++, however, can create Envelope objects that contain a copy of the pointer and the count. When the Envelope object is destroyed, the count is decremented. If the result is zero, the data pointed can can be destroyed. Otherwise, the Envelope object just decrements the count. In C++, when objects are destroyed, a destructor function is called aautomatically and it is here the reference count is decremented.

          These things are called smart, or managed pointers. Check out Design Patterns and look up the Bridge Pattern.

          Comment

          Working...