title: How iOS handles memory corruption from low level C/ Objective-C code?
body: <p>I have an iOS app that uses some legacy low level memory manipulation code using pointers. I'm debugging an issue where multiple threads are causing multiple copies of these code to be executed on global variables simultaneously and cause memory corruption by writing invalid length or overwriting data. </p>
  
  <p>The effect is that the length of the buffer below may change. I've seen iOS throw <code>EXC_BAD_ACCESS</code> or <code>EXC_BREAKPOINT</code> as a result of these calls. </p>
  
  <p>My question is - would iOS always throw exceptions when I use <code>memcpy</code> incorrectly, or will it complain only when I write outside my allowed memory?</p>
  
  <p>In other words, is my code free to corrupt my memory and create invisible issues, without causing exceptions, as long as it does not step outside allocated memory or access deallocated memory?</p>
  
  <pre><code>NSData* buffer = ...
  Byte *array = (Byte*)malloc(buffer.length);
  memcpy(array, buffer.bytes, buffer.length);
  </code></pre>