473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help for writing my own assert()

Hi there,

Can anyone show me how the assert() function works ? I need to develop
my own assert() function instead of using the one defined in the
assert.h file. It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.

I need to write the assert function that prints out error message when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);

Thank you,
Priyanka

Aug 16 '06 #1
13 9371
priyanka wrote:
Hi there,

Can anyone show me how the assert() function works ? I need to
develop
my own assert() function instead of using the one defined in the
assert.h file. It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.

I need to write the assert function that prints out error message
when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);

Thank you,
Priyanka
#define ASSERT(xy,MSG) do \
{ \
if(!(xy))) \
{ \
fprintf(stderr,"Errormessage: %s",
MSG);\
} \
}while(0)

Use: ASSERT(a==b,"My Message to you")

you might insert a call to assert after the fprintf. or simply
exit(EXIT_FAILURE)...

--
Johannes
You can have it:
Quick, Accurate, Inexpensive.
Pick two.
Aug 16 '06 #2
priyanka schrieb:
Can anyone show me how the assert() function works ? I need to develop
assert is a macro.
my own assert() function instead of using the one defined in the
assert.h file. It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.

I need to write the assert function that prints out error message when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);
This version of ASSERT can be switched off just like the
original assert macro via defining NDEBUG; the trick used in
the "Call" of ASSERT to make the assertion print out a
customized error message works for many implementations'
assert as well.

,----
#include <stdio.h>

#define stringize(s) #s
#define XSTR(s) stringize(s)
#if !defined NDEBUG
void abort (void);
# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
# define ASSERT(a) \
do { \
if (0 == (a)) { \
fprintf(stderr, \
"Assertion failed: %s, " \
"%s(), %d at \'%s\'\n", \
__FILE__, \
__func__, \
__LINE__, \
XSTR(a)); \
abort(); \
} \
} while (0)
# else
# define ASSERT(a) \
do { \
if (0 == (a)) { \
fprintf(stderr, \
"Assertion failed: %s, " \
"%d at \'%s\'\n", \
__FILE__, \
__LINE__, \
XSTR(a)); \
abort(); \
} \
} while (0)
# endif
#else
# define ASSERT(a) (void)0
#endif

int main (void)
{
ASSERT(1 == 0
&& "I am the pope");
return 0;
}
`----

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 16 '06 #3
"priyanka" <pr**********@gmail.comwrites:
Can anyone show me how the assert() function works ? I need to develop
my own assert() function instead of using the one defined in the
assert.h file.
Why? Is there something wrong with the predefined assert macro?
It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.
If you have a C implementation, you almost certainly have an assert.h
file that you can look at yourself.
I need to write the assert function that prints out error message when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);
That's what the predefined assert does. Why are you reinventing the
wheel?

(The only reason I can think of to do this is as a homework
assignment. If that's what this is, do it yourself.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 16 '06 #4
Keith Thompson wrote:
(The only reason I can think of to do this [TS: implement a custom assert
macro/fuction] is as a homework assignment.
I have implemented custom asserts more than once, not for homework, but
because:
1) I wanted to capture more information about the environment in which
the failure occurred
2) I was running on an embedded system without stderr/stdout.

--
Thad
Aug 17 '06 #5
On 2006-08-17, Thad Smith <Th*******@acm.orgwrote:
Keith Thompson wrote:
>(The only reason I can think of to do this [TS: implement a custom assert
macro/fuction] is as a homework assignment.

I have implemented custom asserts more than once, not for homework, but
because:
1) I wanted to capture more information about the environment in which
the failure occurred
2) I was running on an embedded system without stderr/stdout.
Both of those require extra code that the OP did not specify that he
wants help with. This looks to be some sort of homework problem.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 17 '06 #6
Andrew Poelstra <ap*******@false.sitewrote:
Both of those require extra code that the OP did not specify that he
wants help with. This looks to be some sort of homework problem.
Seems a moderately pointless one, since the definition is visible in
assert.h.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 17 '06 #7
On 2006-08-17, Christopher Benson-Manica <at***@otaku.freeshell.orgwrote:
Andrew Poelstra <ap*******@false.sitewrote:
>Both of those require extra code that the OP did not specify that he
wants help with. This looks to be some sort of homework problem.

Seems a moderately pointless one, since the definition is visible in
assert.h.
Assuming that
1) assert.h is a file
2) That file can be opened
3) That opened file can be read
4) That opened file, when read, contains a readable definition of assert()

Those three are true on most systems, but it's far from guaranteed.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 17 '06 #8
Andrew Poelstra wrote:
>
On 2006-08-17, Christopher Benson-Manica
<at***@otaku.freeshell.orgwrote:
Andrew Poelstra <ap*******@false.sitewrote:
Both of those require extra
code that the OP did not specify that he
wants help with. This looks to be some sort of homework problem.
Seems a moderately pointless one, since the definition is visible in
assert.h.

Assuming that
1) assert.h is a file
Assumption not necessary:

/* BEGIN new.c */

#include <stdio.h>
#include <assert.h>

#define str(s) # s
#define xstr(s) str(s)

int main(void)
{
puts(xstr(assert(XXXXX)));
return 0;
}

/* END new.c */
--
pete
Aug 17 '06 #9
Andrew Poelstra <ap*******@false.sitewrites:
On 2006-08-17, Christopher Benson-Manica <at***@otaku.freeshell.orgwrote:
>Andrew Poelstra <ap*******@false.sitewrote:
>>Both of those require extra code that the OP did not specify that he
wants help with. This looks to be some sort of homework problem.

Seems a moderately pointless one, since the definition is visible in
assert.h.

Assuming that
1) assert.h is a file
2) That file can be opened
3) That opened file can be read
4) That opened file, when read, contains a readable definition of assert()

Those three are true on most systems, but it's far from guaranteed.
I meant four, of course. I added the fourth point as an afterthought, and
didn't edit my last line.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
Aug 17 '06 #10
Andrew Poelstra <ap*******@false.sitewrote:
Assuming that
1) assert.h is a file
That's true, although I'm curious to know what else the Standard
allows a "header" to be.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 18 '06 #11
Christopher Benson-Manica wrote:
Andrew Poelstra <ap*******@false.sitewrote:
>Assuming that
1) assert.h is a file

That's true, although I'm curious to know what else the Standard
allows a "header" to be.
It doesn't require it to be a file, so its contents (or the meaning
of its contents) could be wired into the compiler.

I /think/ the Norcroft C compiler, at least on RISC OS, did/does this,
with options to allow it to use files instead.

[If the headers are in the compiler, then you don't need header files
somewhere to run the compiler, which is useful if you're juggling
floppy discs or want to be robust against someone reorganising their
hard disc.]

--
Chris "nostalgia" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Aug 18 '06 #12


Christopher Benson-Manica wrote On 08/18/06 09:52,:
Andrew Poelstra <ap*******@false.sitewrote:

>>Assuming that
1) assert.h is a file


That's true, although I'm curious to know what else the Standard
allows a "header" to be.
Anything that produces the effects the Standard calls
for. Case in point: The system headers for the old OpenVMS
VAXC compiler were not files, but "members" of a "library."
(The library was a file, but the individual headers were
not files.)

A visible consequence was in the expansion of the __FILE__
macro. For a header fetched from a library, __FILE__ expanded
to the library's file name with the member name appended,
something like "VAXC$LIBRARY.TLB(STDIO)" (I can't vouch for
the exact syntax any more; it was a long time ago). This
concatenation was not a valid file name; feed the string to
fopen() or something and you'd get nowhere.

VAXC was pre-Standard, but as far as I can see its
behavior in this regard would have been perfectly agreeable
to the Standard when it came along.

--
Er*********@sun.com

Aug 18 '06 #13

"Andrew Poelstra" <ap*******@false.sitewrote in message
news:sl**********************@wpsoftware.net...
On 2006-08-17, Christopher Benson-Manica <at***@otaku.freeshell.org>
wrote:
>Andrew Poelstra <ap*******@false.sitewrote:
>>Both of those require extra code that the OP did not specify that he
wants help with. This looks to be some sort of homework problem.

Seems a moderately pointless one, since the definition is visible in
assert.h.

Assuming that
1) assert.h is a file
2) That file can be opened
3) That opened file can be read
4) That opened file, when read, contains a readable definition of
assert()

Those three are true on most systems, but it's far from guaranteed.
And assuming that the students are sufficiently novices for writing assert()
to be a reasonable exercise, but sufficiently advanced for examining the
system header to be cheating rather than something the instructor is happy
to encourage.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.

Aug 19 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
5
by: squeek | last post by:
i am in a C++ programing class in college (i hate computers because i learn with my hands and can't tear a computer apart to see how it works) and i don't know what i am doing with this class i...
6
by: Jeremy | last post by:
I understand what a unit test is. No problem there. What I'm wondering is what, specifically, do most of you more experienced developers mean by "writing" a unit test. Am I correct to believe...
8
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
8
by: Brett Romero | last post by:
I'd like to use Debug.Assert() but output to a log file instead of the popup window. Can some one provide an example of how that is done? Or will C# only let you use th popup window? Thanks,...
7
by: apollonius2 | last post by:
Greetings, I have been working on a little project today to help me better understand classes in Python (I really like Python). I am a self taught programmer and consider myself to fall in the...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
9
by: pereges | last post by:
Hi, I'm trying to write a macro for the relative difference function which is used to check the close enough floating point values. Is this correct way to write it ? : #define max(x, y) ((x)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.