Welcome! Log In Create A New Profile

Advanced

Asserts ()function

Posted by tshax 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Asserts ()function
July 22, 2009 09:34AM
Hi All,

Does anyone know how to use assert function (Question 2 of Assignment 1 semester 2).
The prescribed has little details about this function and i can't get a clear picture of how does it work or when to use it.

Someone with better understandingconfused smiley Please advise ...
Re: Asserts ()function
July 22, 2009 10:30AM
Language: C++
void assert(int i) { if(i == 0) { cout<<"assertion failed: "<<i<<endl; system("pause"); exit(0); } }
Re: Asserts ()function
July 22, 2009 11:36AM
Hi Randal,

I remember you from COS111U, and thanks for your contribution last semester. But now with the above code, does it mean assertion function holds a prompt message when the boolean condition become false. eye rolling smiley
Re: Asserts ()function
July 22, 2009 11:46AM
yup, try it and see. assert(false) will abend the program. assert(true) will do nothing. So you can use if for something like: assert(a == b || c > d)
Re: Asserts ()function
July 22, 2009 11:59AM
Now I get it...smileys with beer
Re: Asserts ()function
July 23, 2009 01:32PM
Hi Randal,

Its me again, I came up with this code from your explanation.

void triangle(int a, int b, int c, int s, int area)
{
assert(a+b>c && a+c>b && c+b>a);

if (a<0 || b<0 || c<0)
{
s = (a+b+c)/2;
}

area = sqrt( s* (s-a)*(s-b)*(s-c));
}
Please tell me i am on the right track...thumbs up
avatar Re: Asserts ()function
July 23, 2009 10:30PM
@tshax

ASSERT is technically not a function, it's a precompiler macro but for all intents and purposes you can treat it as such.

It's used during the development phase and is a debugging tool. It's supposed to be used to verify conditions and is supposed to crash the code at a known point instead of somewhere unexpected.

As for your example, it doesn't make sense since your system won't crash if your conditions aren't met. More appropriate examples would include checking that the subscript of an array is not out of bounds, or that a you're not dividing by a zero.

ASSERT is defined something like the following. There are most likely extra bits in the definition but they are usually just for providing messages and/or filenames and line numbers

Language: C++
#ifndef NDEBUG #define ASSERT(cond) if (!cond) exit(-1); #else #define ASSERT(cond) #endif


One of the nice things about ASSERT is that, because it's a precompiler macro, and it's under conditional compilation so it can be removed with a single precompiler declaration (usually NDEBUG)

If you have the following code:

Language: C++
float flooberScoop(int side, float area){ ASSERT(side != 0)   return area / side; }


The precompiler would send the following to the compiler:

Language: C++
float flooberScoop(int side, float area){ if (!(side != 0)) exit(-1);   return area / side; }


if you define NDEBUG, the definition of ASSERT is blank and the precompiler sends the following to the compiler:

Language: C++
float flooberScoop(int side, float area){   return area / side; }
Re: Asserts ()function
July 24, 2009 10:15AM
smileHi Robana

I'll try to work on this again with this inforamation and i'll keep you updated.

Thanks
Re: Asserts ()function
August 06, 2009 08:42PM
I just want to know. I have now added the assert function in my function.
If I enter invalid values it gives this error message:

Assertion failed: (sideAP + sideBP > sideCP) && (sideBP + sideCP > sideAP) && (s
ideAP + sideCP > sideBP), file C:\unisa\COS112\Assignment1\Q2.cpp, line 12

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Is this what it is supposed to be?
avatar Re: Asserts ()function
August 06, 2009 10:32PM
ASSERT functions are implimented in different ways by different compilers. Basically the only thing they all have in common is they crash your program. Some of the more useful ones give nice error messages and the point where you program crashed, like the one you've got.
Re: Asserts ()function
August 06, 2009 10:59PM
Thanks robana
Sorry, only registered users may post in this forum.

Click here to login