Re: Debugging standard C library routines



Richard Heathfield posted:

achintmehta@xxxxxxxxx said:

The application/software that is being run, is messing up the memory
due to which a subsequent malloc fails.

Initialise every pointer.


I wouldn't go so far as to advise to initialise _every_ pointer.

#define POVER(arr) ((arr) + sizeof(arr)/sizeof*(arr))

int *p;

for(p=array1;POVER(array1)!=p;++p) /* Something */ ;

for(p=array2;POVER(array2)!=p;++p) /* Something */ ;


Check every malloc/calloc/realloc to ensure it succeeded before you rely
on its return value.


If malloc'ing less than a kilobyte, I wouldn't bother.


Check that every array access is within bounds (0 to n - 1, for an array
of n elements).


One should _always_ watch out for such things when looking back over one's
code.


Set indeterminate ('dangling') pointers to NULL.


In Debug Mode, maybe. It wastes valuable nanoseconds (if not microseconds)
in Release Mode. Maybe something like the following would be a compromise:

#ifdef NDEBUG
#define DMODE(statement)
#else
#define DMODE(statement) statement
#endif

int main()
{
int *p;

...

free(p); DMODE(p=0);
}

--

Frederick Gotham
.



Relevant Pages

  • Re: Debugging standard C library routines
    ... I wouldn't go so far as to advise to initialise _every_ pointer. ... Such stupid advice leads, when followed, ... In Debug Mode, maybe. ...
    (comp.lang.c)
  • Re: sscanf problem.
    ... Read the documentation of sscanf, in particular the section about the "scanf Type Field Characters": ... You are giving sscanf a pointer to a BYTE, not a pointer to an int. ... Maybe it does not fail in debug mode because the memory on the stack aroud the B varriable is not used. ...
    (microsoft.public.vc.mfc)
  • [PATCH 1/1] handle initialising compound pages at orders greater than MAX_ORDER
    ... When we initialise a compound page we initialise the page flags and head ... page pointer for all base pages spanned by that page. ...
    (Linux-Kernel)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)