Thread: Yet another memory question

  1. #1
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115

    Smile Yet another memory question

    I am working on some code and the original coder had a fetish with the assert macro. I mean it is everywhere. Seems he substituted assert() for if statements. Anyways my question is this, seeing as how assert() calls abort(), wouldn't that be cause for memory leaks if memory has been allocated elsewhere in the program? I think it will cause that memory is never freed, which leads into my next question, Should assert() be used this frequently? I mean it isnt uncommon for this program to contain 20 or more assert's in a single function. This is my first real programming job so to speak (I am working for free to get experience) but this many asserts just doesn't seem right.
    I want to change most of these asserts to if statements so that the functions return a value on success or failure and clean up after themselves not just terminate the program. Would this be the correct way to handle things?
    Any help is appreciated
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  2. #2
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Opps forgot to mention that NDEBUG is not defined in any of the files
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assert should be used as a sanity check, it should cover for occurances that should never happen. If this programmer used it in place of if statements then you could very well have memory leaks on your hands if the program asserts situations that happen often.

    >Would this be the correct way to handle things?
    Be absolutely sure that the asserts don't perform a critical function. If you can replace most of those calls with your own assert that collects garbage without effecting the code or introducing bugs then by all means do so. Maintaining code is a job fraught with peril because you can easily create bugs while thinking that you're solving a problem.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Thanks for the advice Prelude. I am not sure if these are sanity checks or not. Like for example, the program has to open some files and the contents of these file should match, if they dont then the program aborts. Now the problem is that a 100 files can be open at once and if one of them fails then the program never closes the ones it has already opened. That sort of thing. Does the same thing with memory allocation as well. Is memory released back to the system once a program terminates or is it still set aside in the system somewhere?
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is memory released back to the system once a program terminates
    That's system dependent. It's almost a sure thing that the big name operating systems will reclaim allocated memory when your program ends but you can't be sure of that.

    >if one of them fails then the program never closes the ones it has already opened
    That's not entirely correct, abort and exit will flush and close any open streams and files when called. It's memory allocated with malloc/calloc/realloc that you need to worry about when it comes to memory leaks.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Thanks Prelude. I didnt know abort would do all that.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 04:34 PM
  2. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM