Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: code tweaks

...

Code Block
#include <stdio.h>
#include <stdlib.h>

int valid(void *p) {
  extern char _etext;
  return (p != NULL) && ((char*) p <= &_etext);
}

int global;

int main(void) {
  int local;
  int *p;

  printf("pointer to local var valid? %d\n", valid(&local));
  printf("pointer to static var valid? %d\n", valid(&global));
  printf("pointer to function valid? %d\n", valid((void *)main));

  p = (int *)malloc(sizeof(int));
  printf("pointer to heap valid? %d\n", valid(p));
  printf("pointer to end of allocated heap valid? %d\n", valid(p++p));
  free(--p);
  printf("pointer to freed heap valid? %d\n", valid(p--));
  printf("null pointer valid? %d\n", valid(NULL));

  return 0;
}

...