title: C int pointer segmentation fault several scenarios, can't explain behaviour
body: <p>I've been fooling around a little with C pointers, and I came up with the following example, but I can't explain the behaviour for each case. Here is the code, but I'm not sure exactly why it behaves the way it does.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void return_void(){
    printf(&quot;in return_no_malloc\n&quot;);
  
    int* p;
    *p = 2;
    printf(&quot;assigned a value for the pointer to point to\n&quot;);
    printf(&quot;Scenario %d\n&quot;, *p);
    printf(&quot;function ends\n\n\n&quot;);
  }
  
  int* return_no_malloc(){
    printf(&quot;in return_no_malloc\n&quot;);
    int* p;
    *p = 3;
    printf(&quot;assigned a value for the pointer to point to\n&quot;);
    printf(&quot;Scenario %d\n&quot;, *p);
    printf(&quot;function ends \n \n \n &quot;);
    return p;
  }
  
  int* return_malloc(){
    printf(&quot;in return_malloc\n&quot;);
    int* p = (int*)malloc(sizeof(int));
    *p = 4;
    printf(&quot;assigned a value for the pointer to point to\n&quot;);
    printf(&quot;Scenario %d\n&quot;, *p);
    printf(&quot;function ends \n \n \n &quot;);
    return p;
  }
  
  
  int main(void) {
    
    //scenario 1: locally declare a pointer and print what it points to 
    // this works (should it?)
    printf(&quot;in main\n&quot;);
    int* p;
    *p = 1;
    printf(&quot;assigned a value for the pointer to point to\n&quot;);
    printf(&quot;Scenario %d\n&quot;, *p);
    printf(&quot;\n\n\n&quot;);
    //////////////////////////////////////////////////////////////////////////
  
  
    //scenario 2: do what you did in scenario 1, but in a helper function
    //return_void(); //causes a seg fault only after exiting the main (why?)
    ///////////////////////////////////////////////////////////////////////////
    
    
    //scenario 3: call a function that returns a pointer to an int that you don't malloc
    //without storing the result of the call
    int* result = return_no_malloc(); //segmentation fault only after exiting main
    ///////////////////////////////////////////////////////////
  
    //scenario 4: use malloc to make space for what the pointer will point to 
    int* q = return_malloc(); //works fine
  
    printf(&quot;main is done \n&quot;);
    //if scenario 2 and 3 are not commented out, the seg fault occurs here
    return 0;
  }
  </code></pre>
  <p>In scenario 1, we declare a pointer to an <code>int</code>, give it the value <code>1</code>, and print. This works. I'm a little confused as to why it works, since we never allocated space for the actual int that <code>p</code> is supposed to point to.</p>
  <p>In scenario 2, we get a segmentation fault after we finish all the instructions in <code>main</code>. Same story for scenario 3. Why in these two scenarios does everything compile and run fine (Still don't know why this is, from scenario 1), but ONLY UNTIL we have no more instructions in <code>main</code>??</p>
  <p>I know that scenario 4 should work, and it does.</p>
  <p>Can someone explain what's going on here? My understanding is that if you want a pointer to point to something, you need to reserve a slot in memory to it.</p>