home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_05 / 1005074c < prev    next >
Text File  |  1991-09-26  |  579b  |  31 lines

  1. /*
  2.  *    Listing 3 -- Error by writing a freed pointer
  3.  *            (another item's value may have changed)
  4.  */
  5.  
  6. #include <stdlib.h>
  7. #include <assert.h>
  8.  
  9. void    main() {
  10. int    *ip1;
  11. int    *ip2;
  12.  
  13.     /* allocate an integer */
  14.     ip1 = malloc(sizeof(int));
  15.     assert(ip1 != NULL);
  16.     (*ip1) = 0;
  17.  
  18.     /* deallocate the integer */
  19.     free(ip1);
  20.  
  21.     /* allocate a second integer */
  22.     ip2 = malloc(sizeof(int));
  23.     assert(ip2 != NULL);
  24.     (*ip2) = 0;
  25.     /* note that ip1 is probably equal to ip2 */
  26.     printf("ip1 == 0x%lx, ip2 == 0x%lx\n",ip1,ip2);
  27.  
  28.     (*ip1) = 5;
  29.     printf("*ip2 == %d\n",*ip2);
  30. }
  31.