title: Should I check if malloc() was successful?
body: <p>Should one check after each <code>malloc()</code> if it was successful? Is it at all possible that a <code>malloc()</code> fails? What happens then?</p>
  <p>At school we were told that we should check, i.e.:</p>
  <pre><code>arr = (int) malloc(sizeof(int)*x*y);
  if(arr==NULL){
      printf(&quot;Error. Allocation was unsuccessful. \n&quot;);
      return 1;
  }
  </code></pre>
  <p>What is the practice regarding this? Can I do it this way:</p>
  <pre><code>if(!(arr = (int) malloc(sizeof(int)*x*y))
      &lt;error&gt;
  </code></pre>