Post without Account — your post will be reviewed, and if appropriate, posted under Anonymous.

Heap variables in C

  • 1 Replies
  • 1397 Views
*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 353
    • View Profile
Heap variables in C
« March 01, 2017, 10:00:05 AM »
One eternal problem when programming in the C language is properly handling pointers into the heap (dynamically allocated memory). It's very easy, if you're not absolutely anal about checking your code, to use unallocated pointers and to fail to free memory when done with it (or to free it too early). The use of three macros and/or library routines would help with this situation:

  • Heap variable (pointer) declarations: don't simply declare them like any other variable, but also force them to be initialized to NULL. The purpose is to make sure a pointer doesn't have non-NULL garbage in it if it should be used too early, that would fool the runtime into not flagging it.
  • Wrapper around malloc/calloc calls: first check if the pointer variable being written to is NULL. The purpose is to avoid accidentally overwriting a pointer already in use, resulting in the loss (memory leak) of whatever it was pointing to. Only NULL pointers should be written to. It may also be possible to automatically cast the returned pointer to the same type as in the declaration, but that is left as an exercise for the reader.
  • Freeing the pointer: do not call free() directly, but in some sort of wrapper, so that after the heap memory is freed, the pointer variable is set to NULL. This will permit the pointer to be reused and not flagged as "still in use" by the *alloc call.

Numbers 2 and 3 are pretty easy to implement as C routines, but number 1 is a macro that I never really got the hang of getting to work in a really general manner.  It would be nice to keep track of the pointer declaration (type), so that the *alloc call could automatically cast the pointer to the right type, but that may be beyond the regular C preprocessor. Perhaps a new preprocessor could be written that could handle this, and other improvements?

Update: Expanded into an article on pointer usage. This topic is the discussion for that article.
« Last Edit: March 11, 2017, 05:37:52 PM by Phil »

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 353
    • View Profile
Re: Heap variables in C
« Reply #1: May 08, 2017, 11:14:25 AM »
A preprocessor might call a check routine at the end of scope for a declared pointer value, and issue a run time warning if a non-NULL pointer is about to go out of scope (i.e., become a memory leak). This could also be done manually, but would depend upon the vigilance of the programmer (remember to test all pointers). For heap pointers, they should be NULL at exit, having either been freed, or copied back up to the caller (preserved) and set to NULL (not freed, though) in this routine (to avoid tripping the leak test). If the heap pointer itself is a parameter, it may need a flag to tell the preprocessor not to worry about the block ending with a non-NULL value. For other pointers, it may or may not matter that they are going out of scope with values.

The safest implementation would be to always make such a check, but it would not be unreasonable (for performance reasons) to only do this during testing and debugging, and hope that all problems have been caught by this time!