C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-15-2002, 11:53 PM   #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
Dohojar is offline   Reply With Quote
Old 03-16-2002, 12:02 AM   #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
Dohojar is offline   Reply With Quote
Old 03-16-2002, 12:06 AM   #3
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
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.
Prelude is offline   Reply With Quote
Old 03-16-2002, 12:22 AM   #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
Dohojar is offline   Reply With Quote
Old 03-16-2002, 10:15 AM   #5
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>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.
Prelude is offline   Reply With Quote
Old 03-16-2002, 01:47 PM   #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
Dohojar is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:51 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22