9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] kenc: casting expression to void not ignored
@ 2008-06-29 13:53 Pietro Gagliardi
  2008-06-29 13:57 ` Pietro Gagliardi
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Pietro Gagliardi @ 2008-06-29 13:53 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hello. I have a simple push macro for a stack machine interpreter. It  
looks like this:

	#define push(s) ((void)(((stackptr >= (stack + stacksize)) ?  
growstack() : (void)0), (*stackptr++ = errcheck(s))))

Does this boggle the mind? It's very simple: it checks for stack  
overflow and makes the stack bigger if so, then checks to see if s, a  
double, is NaN or ±Inf, and finally pushes it onto the stack if not  
(errcheck calls longjmp - how I wish I had try).

However, when I use the push macro, I get tons of warnings like this:

	warning: run.c:170 result of operation not used

Shouldn't the cast to void mean that I don't intend to use the result  
of the operation? Thanks.




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

* Re: [9fans] kenc: casting expression to void not ignored
  2008-06-29 13:53 [9fans] kenc: casting expression to void not ignored Pietro Gagliardi
@ 2008-06-29 13:57 ` Pietro Gagliardi
  2008-06-29 14:18 ` erik quanstrom
  2008-06-29 15:28 ` Rob Pike
  2 siblings, 0 replies; 4+ messages in thread
From: Pietro Gagliardi @ 2008-06-29 13:57 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

I forgot to mention this is with the -w option; I found that turning  
it off also hid warnings I wanted to keep.

On Jun 29, 2008, at 9:53 AM, Pietro Gagliardi wrote:

> Hello. I have a simple push macro for a stack machine interpreter.  
> It looks like this:
>
> 	#define push(s) ((void)(((stackptr >= (stack + stacksize)) ?  
> growstack() : (void)0), (*stackptr++ = errcheck(s))))
>
> Does this boggle the mind? It's very simple: it checks for stack  
> overflow and makes the stack bigger if so, then checks to see if s,  
> a double, is NaN or ±Inf, and finally pushes it onto the stack if  
> not (errcheck calls longjmp - how I wish I had try).
>
> However, when I use the push macro, I get tons of warnings like this:
>
> 	warning: run.c:170 result of operation not used
>
> Shouldn't the cast to void mean that I don't intend to use the  
> result of the operation? Thanks.
>
>




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

* Re: [9fans] kenc: casting expression to void not ignored
  2008-06-29 13:53 [9fans] kenc: casting expression to void not ignored Pietro Gagliardi
  2008-06-29 13:57 ` Pietro Gagliardi
@ 2008-06-29 14:18 ` erik quanstrom
  2008-06-29 15:28 ` Rob Pike
  2 siblings, 0 replies; 4+ messages in thread
From: erik quanstrom @ 2008-06-29 14:18 UTC (permalink / raw)
  To: 9fans

> Hello. I have a simple push macro for a stack machine interpreter. It
> looks like this:
>
> 	#define push(s) ((void)(((stackptr >= (stack + stacksize)) ?
> growstack() : (void)0), (*stackptr++ = errcheck(s))))
>
> Does this boggle the mind? It's very simple: it checks for stack
> overflow and makes the stack bigger if so, then checks to see if s, a
> double, is NaN or ±Inf, and finally pushes it onto the stack if not
> (errcheck calls longjmp - how I wish I had try).
>
> However, when I use the push macro, I get tons of warnings like this:
>
> 	warning: run.c:170 result of operation not used
>
> Shouldn't the cast to void mean that I don't intend to use the result
> of the operation? Thanks.

no.  USED(x) where x is a variable name means you do not intend to use
the result of the operation. why don't you write push like this instead?

void
push(Whatever *s)
{
	if(stackptr >= stack + stacksize)
		growstack();
	*stackptr++ = errcheck(s)
}

this eliminates two wierd void casts, a ternary operator,
a comma operator and your warning.  i find it
difficult to imagine that a function call would be significant
overhead in this case.

on the certainly wrong assumption that the function call
needs to be avoided, there's no rule that one can't use if
statements in macros:

#define push(s)\
	do {\
		if(stackptr >= stack + stacksize)\
			growstack;\
		*stackptr++ = errcheck(s);\
	} while(0)

but for pete's sake, use a function until you know it's
too slow.

- erik




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

* Re: [9fans] kenc: casting expression to void not ignored
  2008-06-29 13:53 [9fans] kenc: casting expression to void not ignored Pietro Gagliardi
  2008-06-29 13:57 ` Pietro Gagliardi
  2008-06-29 14:18 ` erik quanstrom
@ 2008-06-29 15:28 ` Rob Pike
  2 siblings, 0 replies; 4+ messages in thread
From: Rob Pike @ 2008-06-29 15:28 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> Shouldn't the cast to void mean that I don't intend to use the result of the
> operation? Thanks.

That's not standard C, just a bad convention established by lint(1)
and carried too far for a while before fading away again.

-rob



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

end of thread, other threads:[~2008-06-29 15:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-29 13:53 [9fans] kenc: casting expression to void not ignored Pietro Gagliardi
2008-06-29 13:57 ` Pietro Gagliardi
2008-06-29 14:18 ` erik quanstrom
2008-06-29 15:28 ` Rob Pike

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).