9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] what's wrong with this?
@ 2002-09-27 14:05 Russ Cox
  0 siblings, 0 replies; 10+ messages in thread
From: Russ Cox @ 2002-09-27 14:05 UTC (permalink / raw)
  To: 9fans

The problem is that assert tries to include the
text of what is being asserted, since the compiler
doesn't provide preprocessor variables for the
current file and line number.

Russ


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
@ 2002-10-07 14:34 Russ Cox
  0 siblings, 0 replies; 10+ messages in thread
From: Russ Cox @ 2002-10-07 14:34 UTC (permalink / raw)
  To: 9fans

the correct workaround is

#define assert(x) ((x) ? 0 : (_assert("x"), 0))



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
  2002-09-27 14:09 Sape Mullender
@ 2002-10-07 13:53 ` peter a. cejchan
  0 siblings, 0 replies; 10+ messages in thread
From: peter a. cejchan @ 2002-10-07 13:53 UTC (permalink / raw)
  To: 9fans

sape@plan9.bell-labs.com (Sape Mullender) wrote in message
[snip]
> For starters, it's abhorrently awful code.  You're also using macros recursively.
> Assert is a macro:
> 	#define	assert(x) if(x){}else _assert("x")
> Substitute it in your code and behold the mess you get.
>
> What are you trying to save with this macro anyway?
>
> 	Sape

eeeeeeeeeeeeeeeeeeeeeeeh! it isn't my code... just trying to ape-port
someone else's (jpeg-2000 standard codec (i depend on it with my
hi-quality compressed images)). could you, please, show me a correct
workaround (for the time being i just dropped assert()) ???

TIA, cheers,
++pac.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
  2002-09-27 14:28 Charles Forsyth
  2002-09-30  9:17 ` Douglas A. Gwyn
@ 2002-09-30  9:28 ` Ralph Corderoy
  1 sibling, 0 replies; 10+ messages in thread
From: Ralph Corderoy @ 2002-09-30  9:28 UTC (permalink / raw)
  To: 9fans

Hi Charles,

> i was wondering whether the C assert macro was regarded as
> an expression or a statement, but as usual my C book isn't
> nearby.

Harbison and Steele say it acts as if it was

    void assert(int expr);

and if NDEBUG is #define'd then an implementation may #define assert to
be

    ((void)0)

Cheers,


Ralph.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
  2002-09-27 15:36 rog
@ 2002-09-30  9:18 ` Douglas A. Gwyn
  0 siblings, 0 replies; 10+ messages in thread
From: Douglas A. Gwyn @ 2002-09-30  9:18 UTC (permalink / raw)
  To: 9fans

rog@vitanuova.com wrote:
> i always thought that:
> #define assert(x)       if(x){}else _assert("x")
> was a bit dodgy...

Yes, it should be:

extern void _assert(const char*);
#define	assert(x)	((x)?(void)0:_assert(#x))


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
  2002-09-27 14:28 Charles Forsyth
@ 2002-09-30  9:17 ` Douglas A. Gwyn
  2002-09-30  9:28 ` Ralph Corderoy
  1 sibling, 0 replies; 10+ messages in thread
From: Douglas A. Gwyn @ 2002-09-30  9:17 UTC (permalink / raw)
  To: 9fans

Charles Forsyth wrote:
> i was wondering whether the C assert macro was regarded as
> an expression or a statement, but as usual my C book isn't
> nearby.

The C standard requires that it expand to a void expression,
which of course should not have the -> operator applied to it.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
@ 2002-09-27 15:36 rog
  2002-09-30  9:18 ` Douglas A. Gwyn
  0 siblings, 1 reply; 10+ messages in thread
From: rog @ 2002-09-27 15:36 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 243 bytes --]

i always thought that:

#define	assert(x)	if(x){}else _assert("x")

was a bit dodgy... it's a good thing macros aren't
used much in plan 9 otherwise someone would
have been bitten.

still it's better than the ## nastiness, i suppose.

[-- Attachment #2: Type: message/rfc822, Size: 1519 bytes --]

From: "Russ Cox" <rsc@plan9.bell-labs.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] what's wrong with this?
Date: Fri, 27 Sep 2002 10:05:43 -0400
Message-ID: <b70f510fee137a3d8569f447c6f762e3@plan9.bell-labs.com>

The problem is that assert tries to include the
text of what is being asserted, since the compiler
doesn't provide preprocessor variables for the
current file and line number.

Russ

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
@ 2002-09-27 14:28 Charles Forsyth
  2002-09-30  9:17 ` Douglas A. Gwyn
  2002-09-30  9:28 ` Ralph Corderoy
  0 siblings, 2 replies; 10+ messages in thread
From: Charles Forsyth @ 2002-09-27 14:28 UTC (permalink / raw)
  To: 9fans

i was wondering whether the C assert macro was regarded as
an expression or a statement, but as usual my C book isn't
nearby.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [9fans] what's wrong with this?
@ 2002-09-27 14:12 Peter A. Cejchan
  0 siblings, 0 replies; 10+ messages in thread
From: Peter A. Cejchan @ 2002-09-27 14:12 UTC (permalink / raw)
  To: 9fans

Dear friends,

8c complies "syntax error" when the following macro is called:

#define	jpc_bitstream_getbit_macro(bitstream) \
	(assert((bitstream)->openmode_ & JPC_BITSTREAM_READ), \
	  (--(bitstream)->cnt_ >= 0) ? \
	  (((bitstream)->buf_ >> (bitstream)->cnt_) & 1) : \
	  jpc_bitstream_fillbuf(bitstream))

int ret;
...
ret=jpc_bitstream_getbit_macro(bitstream);


TIA4help,
--
++pac.

Peter A. Cejchan
Paleobiology Lab, GLU Acad. Sci. CZ
<pac@next.gli.cas.cz>
[http | ftp]://next.gli.cas.cz


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] what's wrong with this?
@ 2002-09-27 14:09 Sape Mullender
  2002-10-07 13:53 ` peter a. cejchan
  0 siblings, 1 reply; 10+ messages in thread
From: Sape Mullender @ 2002-09-27 14:09 UTC (permalink / raw)
  To: 9fans

> Dear friends,
>
> 8c complies "syntax error" when the following macro is called:
>
> #define	jpc_bitstream_getbit_macro(bitstream) \
> 	(assert((bitstream)->openmode_ & JPC_BITSTREAM_READ), \
> 	  (--(bitstream)->cnt_ >= 0) ? \
> 	  (((bitstream)->buf_ >> (bitstream)->cnt_) & 1) : \
> 	  jpc_bitstream_fillbuf(bitstream))
>
> int ret;
> ...
> ret=jpc_bitstream_getbit_macro(bitstream);

For starters, it's abhorrently awful code.  You're also using macros recursively.
Assert is a macro:
	#define	assert(x) if(x){}else _assert("x")
Substitute it in your code and behold the mess you get.

What are you trying to save with this macro anyway?

	Sape



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2002-10-07 14:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-27 14:05 [9fans] what's wrong with this? Russ Cox
2002-09-27 14:09 Sape Mullender
2002-10-07 13:53 ` peter a. cejchan
2002-09-27 14:12 Peter A. Cejchan
2002-09-27 14:28 Charles Forsyth
2002-09-30  9:17 ` Douglas A. Gwyn
2002-09-30  9:28 ` Ralph Corderoy
2002-09-27 15:36 rog
2002-09-30  9:18 ` Douglas A. Gwyn
2002-10-07 14:34 Russ Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).