Newsgroups: comp.os.msdos.programmer
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!wuarchive!uunet!proto!joe
From: joe@proto.com (Joe Huffman)
Subject: Re: HELP!!! - code executes different on same data
Organization: Prototronics @ Sandpoint, Idaho
Date: Sat, 25 May 1991 00:03:26 GMT
Message-ID: <1991May25.000326.17223@proto.com>
References: <1991May23.131907.11319@unlinfo.unl.edu>
Keywords: HELP

khawand@hoss.unl.edu (Nancy Khawand) writes:

>I am using MSC to write a macro interface to Harvard Graphics - no
>big deal.  The problem however is that when I execute my code I get
>different results from the same data each time it is run.  Sometimes
>the program works as I expect and sometimes it crashes the machine.
>Removing printf debug statments also seems to affect the outcome.

Pointer bug(s) (of course).  Using the following program to set
almost all of your memory to a known value will probably help you 
get reproducible results which might help tracking it down.

There are memory debug package that are available that help with this sort
of problem too.  Zortech supplies one with their compiler...

----------
/* setmem.c   Tue Jan 13 1987  Written by: Joe Huffman  */
/* Program to set all free memory to a specified value.	*/
/* Useful for debugging programs with pointer bugs.	*/
/* Use:
 *	SETMEM		;set all memory to 0
 *	SETMEM value	;set all memory to value (in hex)
 */

#include <stdio.h>

main (argc, argv)
int argc;
char *argv[];
{
  unsigned int val;
  unsigned long allcoreleft (unsigned int);

  if (argc > 1)
    sscanf (argv[1], "%x", &val);
  else
    val = 0;

  printf ("%ld bytes set to 0x%x.", allcoreleft (val), val);
}
/*****************************************************************************
Return the maximum number of bytes of objects of any size can be allocated.  
And sets that data to val.
*****************************************************************************/
unsigned long allcoreleft (val)
unsigned int val;
{
  char *ptr = (char *)NULL, *malloc(unsigned int);
  char *memset (char *, unsigned int, unsigned int);
  unsigned int coreleft (void), subtotal;
  unsigned long int total;

  _chkstack ();
  total = subtotal = coreleft ();
  if (subtotal > 0)
  {
    ptr = malloc (subtotal);
    total += allcoreleft (val);
    if (ptr)
    {
      memset (ptr, val, subtotal);
      free (ptr);
    }
    else
    {
      fputs ("Bug in \"allcoreleft\"", stderr);
      exit (1);
    }
  }
  return total;
}
/*****************************************************************************
Return the number of bytes left on the heap obtainable in one array.
*****************************************************************************/
unsigned int coreleft (void)
{
  unsigned int core = 0x8000, step = 0x8000;
  char *ptr = (char *)NULL, *malloc(unsigned int);

  while (step)
  {
    ptr = malloc (core);
    if(ptr)
      free (ptr);
    else
      core -= step;
    step >>= 1;
    core += step;
  }
  return core;
}

-- 
joe@proto.com
