Is Assert() useful?
Ive been programming in c/c++ for years, but ive never been bothered about the Assert().!.
1) What does the Assert() function do anyway?
2) How many of u use it?
3) Should I use it in my proggy?
4) Where should I use it?
Downloads: ZeroOne Realm
Since no one else is going to take this one:
1) Checks if dynamic allocation is valid
2) ???
3) Depends, should you use classes? structs? namespaces? etc? Depends on what you do and how you do it...
4) Wherever dynamic allocation occurs, see #1
Points 1 and 4 can be answered with your compilers help file.
Points 2 and 3 can be answered by examining full blown source code of something beyond a Hello World! application.
( I just realized that my last statement about points 2 and 3 sounded kind of rude )
I was addressing something besides sample apps and code snippets, but rather full blown software that uses multiple ideas and language syntax.
I was not attempting to imply that you have only done Hello World! apps. Hopefully it wasn't taken as such.
Edited by - deadlinegrunt on 2/28/00 1:51:41 AM
1) Checks if dynamic allocation is valid
2) ???
3) Depends, should you use classes? structs? namespaces? etc? Depends on what you do and how you do it...
4) Wherever dynamic allocation occurs, see #1
Points 1 and 4 can be answered with your compilers help file.
Points 2 and 3 can be answered by examining full blown source code of something beyond a Hello World! application.
( I just realized that my last statement about points 2 and 3 sounded kind of rude )
I was addressing something besides sample apps and code snippets, but rather full blown software that uses multiple ideas and language syntax.
I was not attempting to imply that you have only done Hello World! apps. Hopefully it wasn't taken as such.
Edited by - deadlinegrunt on 2/28/00 1:51:41 AM
~deadlinegrunt
assert() can be used to check the validity of any boolean expression. It commonly used to make sure that parameters, such as pointer, are what they are expected to be.
assert() is a tool for debugging, as it is turned off in the release code (without manual modification of the code) by defining NDEBUG.
assert() is a tool for debugging, as it is turned off in the release code (without manual modification of the code) by defining NDEBUG.
I would disagree about using asserts to check dynamic allocation. asserts (assert is a standard, Assert is Win specific. Don''t know the difference, but assert works fine, so I''d suggest using that one) should not be used where the program can truely have an error. They are used to check things that should never happen, and to assure that everything is acting the way it should.
EX:
If a function is an internal function, and its passing a pointer to be filled in, you can assert to make sure the pointer is legal (it always should be). If it can be called externally, then it should do a real check on the pointer and fail gracefully.
Use it to check logic also. For example, in a Release type function, where you''re keeping track of how many objects are allocated/released, you can decrement a reference count, and assert that it isn''t < 0, which it should never be.
Personally, I LOVE asserts, and advocate using them everywhere (these are just a few examples). Just remember that they aren''t present in the final code, so anything that may really fail (opening a file, memory allocation, DX init, ....) has to be checked and handled normally.
Rock
EX:
If a function is an internal function, and its passing a pointer to be filled in, you can assert to make sure the pointer is legal (it always should be). If it can be called externally, then it should do a real check on the pointer and fail gracefully.
Use it to check logic also. For example, in a Release type function, where you''re keeping track of how many objects are allocated/released, you can decrement a reference count, and assert that it isn''t < 0, which it should never be.
Personally, I LOVE asserts, and advocate using them everywhere (these are just a few examples). Just remember that they aren''t present in the final code, so anything that may really fail (opening a file, memory allocation, DX init, ....) has to be checked and handled normally.
Rock
2) I use it
3) yes
4) Use to validate any assumptions. Typically used to validate parameters to a function or a fucntion return value. very simplified example.
int getBitmapWidth(Bitmap *ptr)
{
Assert(ptr != NULL); // should not be passing NULL ptr
Assert(ptr->width >= 0); // no bitmap should have negative width;
return ptr->width;
}
3) yes
4) Use to validate any assumptions. Typically used to validate parameters to a function or a fucntion return value. very simplified example.
int getBitmapWidth(Bitmap *ptr)
{
Assert(ptr != NULL); // should not be passing NULL ptr
Assert(ptr->width >= 0); // no bitmap should have negative width;
return ptr->width;
}
--Rick
TribesPlayers.com
TribesPlayers.com
I would agree with you as well Rock. The original post led me to believe that was geared about Win specific ( Assert() !assert() ). That being said, they ( being MS ) push it for dynamic memory allocation/instanciation in debug mode more than anything else, albeit not the only thing. Hence the comment on full blown source.
All that in mind, I also agree with your post completely. If you still feel that my post was in err, my bad then. However, I still feel the bigger point I was trying to make wasn''t in err which was to turn to source for ultimate answers.
All that in mind, I also agree with your post completely. If you still feel that my post was in err, my bad then. However, I still feel the bigger point I was trying to make wasn''t in err which was to turn to source for ultimate answers.
~deadlinegrunt
Two golden rules about assert()''s:
1) use them early, use them often.
2) Only use them on things you assume to be true.
Memory allocation is not something you can be assume to be true - even on modern virtual memory OS''s you CAN run out of memory (i.e. no more disk space to grow swap file).
The way to think about it is "is this an error or an assumption?" An error is something that you didn''t expect to happen but can sometimes happen (like a file not existing, or failing to make a network connection). An assumption is something that CAN NEVER HAPPEN. EVER. If it does happen, it''s a bug. If this is the case, use an assert.
-vince
1) use them early, use them often.
2) Only use them on things you assume to be true.
Memory allocation is not something you can be assume to be true - even on modern virtual memory OS''s you CAN run out of memory (i.e. no more disk space to grow swap file).
The way to think about it is "is this an error or an assumption?" An error is something that you didn''t expect to happen but can sometimes happen (like a file not existing, or failing to make a network connection). An assumption is something that CAN NEVER HAPPEN. EVER. If it does happen, it''s a bug. If this is the case, use an assert.
-vince
-vince
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement