title: Is Dynamically allocated memory global?
body: <p>I am wondering if dynamically allocated memory with malloc global? I am reading online that allocated memory with malloc is stored on the heap. I also read online that all global variables are stored on the heap. Wouldn't this mean that dynamically allocated memory can be accessed globally? For example, I receive an error with the following code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void my_func(void)
  {
      printf(&quot;Pointer variables is: %d\n&quot;, *ptr);
  }
  
  int main()
  {
      int *ptr = (int *)malloc(sizeof(int));
      *ptr = 5;
      my_func();
      return 0;
  }
  </code></pre>
  <p>However, when i run the following code with a global variable there is no error:</p>
  <pre><code>#include &lt;stdio.h&gt;
  int var = 5;
  
  void my_func(void)
  {
      printf(&quot;Global variable is: %d\n&quot;, var);
  }
  
  int main()
  {
      my_func();
      return 0;
  }
  </code></pre>