How this peice of code will execute?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjuks
    New Member
    • Dec 2007
    • 72

    How this peice of code will execute?

    Hi All,

    What is happening in the below piece of code
    Its printing Hello, why not world. Its working like call by value. Please clarify me on this.

    int main()
    {
    char *temp = (char *)malloc(sizeof ("Hello" + 1));
    strcpy(temp, "Hello");
    fun(temp);
    printf("temp = %s\n", temp);
    return 0;
    }
    void fun(char *temp)
    {
    temp = (char *)malloc(sizeof ("World" + 1));
    strcpy(temp, "World");
    }
  • solita
    New Member
    • Jun 2009
    • 17

    #2
    I think you should use the following format.

    func(&temp)

    and

    void fun(char **temp)


    int main()
    {
    char *temp = (char *)malloc(sizeof ("Hello" + 1));
    strcpy(temp, "Hello");
    fun(&temp);
    printf("temp = %s\n", temp);
    return 0;
    }
    void fun(char **temp)
    {
    *temp = (char *)malloc(sizeof ("World" + 1));
    strcpy(*temp, "World");
    }

    I am new to programming so I may be wrong.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      manjuks, it is working as call by value because it is call by value. C has no call by reference. C++ does but that is not the syntax, that is call by value passing a pointer to char.

      Your function, fun, takes a pointer. When it is called the value of a pointer to passed to the function. The code immediately overwrites this value with one of its own storing it in the local parameter temp and then copies data to that new location. The function then exits leaking the memory allocated because it is never freed. Back in main none of the variable values have changed value because C calls by value. The data pointed to by temp hasn't changed because the function, fun, never used the pointer that was passed to it, it overwrote it.

      Like I said C only supports call by value. It does support a pseudo call by reference by passing a pointer by value. The called function can then use the pointer to access the calling codes copy of what was pointed to. For an arbitrary type T then

      void fun(T *param);

      is a function that uses call by value to pass the value of a pointer T * in param. The function can then use param to achieve a pseudo call by reference to a type T in the calling code.

      T value = <TInitialiser >;

      fun(&value);

      The address of value is passed in call by value to the fun but fun can use that pointer to achieve a pseudo call by reference to value.

      You wanted to call by reference a variable of type char *. Looking at my example that means that T has type char *, the function has a parameter type type T * so that would be char **.

      To get pseudo call by reference to a variable of type char * the function needs to take a parameter of type char **. That is exactly what solitas example shows.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        By the way, your program calls malloc() twice but never calls free(). This doesn't cause a problem for such a simple program, but it is a bad habit to get into. You should develop the discipline to always free dynamically allocated memory once you're finished with it.

        Comment

        • manjuks
          New Member
          • Dec 2007
          • 72

          #5
          Hi,

          Thanks a lot for your suggestions. I understood this properly.

          Thanks,
          Manjunath

          Comment

          Working...