title: malloc preventing garbage from being printed?
body: <p>Program was programmed in C and compiled with GCC.</p>
  
  <p>I was trying to help a friend who was trying to use trying to (shallow) copy a value that was passed into a function. His the value was a struct that held primitives and pointers (no arrays or buffers). Unsure of how malloc works, he used it similar to how the following was done:</p>
  
  <pre>
  void some_function(int rand_params, SOME_STRUCT_TYPEDEF *ptr){
      SOME_STRUCT_TYPEDEF *cpy;
      cpy = malloc(sizeof(SOME_STRUCT_TYPEDEF));// this line makes a difference?!?!?
      cpy = ptr;// overwrites cpy anyway, right?
      //prints a value in the struct documented to be a char*,
      //sorry couldn't find the documentation right now
  }
  </pre>
  
  <p>I told him that the malloc shouldn't affect the program, so told him to comment it out. To my surprise, the malloc caused a different output (with some intended strings) from the implementation with the malloc commented out (prints our garbage values). The pointer that's passed into the this function is from some other library function which I don't have documentation for at the moment. The best I can assume it that the pointer was for a value that was actually a buffer (that was on the stack). But I still don't see how the malloc can cause such a difference. Could someone explain how that malloc may cause a difference?</p>