[comp.os.msdos.programmer] HELP!!! - code executes different on same data

khawand@hoss.unl.edu (Nancy Khawand) (05/23/91)

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.

I talked with MS and they said to recompile the program disabling
all optimizations.  When I do that, the programs doesn't work at all.

Does anyone have any suggestions as to what to look for - I'm one
lost sole on this one.


--
Nancy Khawand				Internet: khawand@hoss.unl.edu
University of Nebraska - Lincoln        Phone   : (402) 472-5663
Computing Resource Center               FAX     : (402) 472-5280

oneel@heawk1.gsfc.nasa.gov ( Bruce Oneel ) (05/23/91)

In article <1991May23.131907.11319@unlinfo.unl.edu> 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.

   I talked with MS and they said to recompile the program disabling
   all optimizations.  When I do that, the programs doesn't work at all.

   Does anyone have any suggestions as to what to look for - I'm one
   lost sole on this one.


Ahh, the joys of C.  Are you using 6.0?  If so, are you using function
prototypes?  It's worth the time to put them in.  You may have botched
calling a function.  The other worth guess is that you've botched a
pointer and something is getting overwritten.  When adding/removing
statements which shouldn't affect the code (such as printf) have
effects then that may be the problem.  If it fits, can you try the
small model?  That might at least stop over-writing the code portion
of your program.  You can also try the (medium,compact) model, which
ever gives you 64k data, but 1 meg code.

Good luck.

bruce.

p.s. Sometimes, Fred Brook's advice is a good idea.  You learned quite
a bit from the first try.  Start over fresh.  It'll probably work
better.
--
Bruce O'Neel              oneel@heasfs.gsfc.nasa.gov
NASA/GSFC/STX/Code 664

joe@proto.com (Joe Huffman) (05/25/91)

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

Bits_of_Magic@cup.portal.com (05/26/91)

Also check to see if you are using uninitialized data.  Particularly if you
find the program behaves consistently following a power-on.

	evan