title: mprotect - how aligning to multiple of pagesize works?
body: <p>I am not understanding the 'aligning allocated memory' part from the mprotect usage.</p>
  
  <p>I am referring to the code example given on <a href="http://linux.die.net/man/2/mprotect" rel="nofollow noreferrer">http://linux.die.net/man/2/mprotect</a></p>
  
  <pre><code>char *p;
  char c;
  /* Allocate a buffer; it will have the default
     protection of PROT_READ|PROT_WRITE. */
  p = malloc(1024+PAGESIZE-1);
  if (!p) {
      perror("Couldn't malloc(1024)");
      exit(errno);
  }
  /* Align to a multiple of PAGESIZE, assumed to be a power of two */
  p = (char *)(((int) p + PAGESIZE-1) &amp; ~(PAGESIZE-1));
  c = p[666];         /* Read; ok */
  p[666] = 42;        /* Write; ok */
  /* Mark the buffer read-only. */
  if (mprotect(p, 1024, PROT_READ)) {
      perror("Couldn't mprotect");
      exit(errno);
  }
  </code></pre>
  
  <p>For my understanding, I tried using a PAGESIZE of 16, and 0010 as address of p. 
  I ended up getting 0001 as the result of <code>(((int) p + PAGESIZE-1) &amp; ~(PAGESIZE-1))</code>.</p>
  
  <p>Could you please clarify how this whole 'alignment' works?</p>
  
  <p>Thanks,</p>