Dave's Brain

Browse - programming tips - noop in c or c++

Date: 2010may3
Language: C/C++
Keywords: no-op

Q.  How can I do an assembler-like NOOP in C/C++ ?

A.  If you really want to do nothing you can do:

	#define NOOP		/* do nothing */

	ExampleUse()
	{
		NOOP
	}

or

	// Notice there is no trailing semicolon
	#define NOOP_FUNCTION	do { /* do nothing */ } while (false)

	ExampleUse()
	{
		NOOP_FUNCTION();
		// This might not get compiled down to nothing
	}

or 

	inline void Noop()
	{
		/* do nothing */
	}

	ExampleUse()
	{
		Noop();
	}

or

	In some environments, in <stdarg.h> you have:

	#define va_end(ap)          ((void)0)

	which does very little.

	ExampleUse()
	{
		va_end(ap);
	}

But sometimes you want a very small time delay.  So you can try:

	true;

	ExampleUse()
	{
		true;		// Sleep a bit
	}

In Windows you can do:

	Sleep(0);

	Which appears to do very little.  It will sleep zero milliseconds
	and but also give control to another process if it is waiting.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.