• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
swiftcoder

goto considered harmful within global macros

21 posts in this topic

I was really quite surprised when I first attempted to add error checking at work, and my compilation failed with a [tt]error: use of undeclared label 'onError'[/tt].

 

From a ubiquitous global header in a (thankfully recently deceased) library within our project:

#define CheckSuccess(condition) do { \
    if (!condition) { \
        goto onError; \
    } \
} while (0) 

The idea that an error checking macro would require the current function to implement a specifically named label for error cleanup (or, indeed, that there would be significant error cleanup not handled by RAII), is a little unpleasant to me...

 

I should probably add that the author of this particular gem thought that they were working around the lack of exception handling on our target platform.

0

Share this post


Link to post
Share on other sites

Meh, macros like this are common in C programs. Also popular:

#define CHECK(x) do { if(err = (x)) goto error; } while(0)

to save the actual error code (and assuming 0 = no error), which requires a variable of the appropriate type in addition to the proper goto label.

 

It should probably not be defined in a global header though.

Edited by l0calh05t
0

Share this post


Link to post
Share on other sites

I'm 100% surprised a velociraptor attack did not ensue. That macro only saves you from typing 7 keystrokes too... totally not worth using it IMO. And I hate macros like that for debugging and stepping through code.

 

Also, condition really should've been in parentheses (like in l0calh05t's post), because other wise things like CheckSuccess(false || true) would've resulted in madness.

2

Share this post


Link to post
Share on other sites

 

The real WTF is that other half of the error handling system isn't hidden behind an equivalent macro laugh.png tongue.png

#define CheckSuccess(condition) do { \
	if (!condition) { \
			goto onError; \
		} \
	} while (0) 

#define OnError do { if(false) { \
	onError: (void)0
#define EndError }} while (0) 

 

WTF Indeed blink.png .

0

Share this post


Link to post
Share on other sites

On the rare occasions I need macros that are specific to a function or small set of related functions, I define them in the source file and then #undef them afterward. I've yet to put a goto in one though... blink.png

0

Share this post


Link to post
Share on other sites

Pfft, I once ended a macro with an "else" (and nothing afterwards).

 

It was my first job though so I didn't know much better.

 

I had a load of check for joypad presses all over the code (bad I know). Then we got the standards guidelines through and pressing select and start for 3 seconds had to return to the menu. So I hacked it with a dodgy macro ;) That was when I used longjmp to return from anywhere back to the main menu too.

0

Share this post


Link to post
Share on other sites


That was when I used longjmp to return from anywhere back to the main menu too.

That's deliciously evil, but it's worth keeping in mind that it isn't unprecedented.

 

Your longjmp is effectively just implementing a call/cc, and would be considered a (fairly) sane method of control flow in most functional languages.

0

Share this post


Link to post
Share on other sites

It's pretty similar to using exceptions (it resets the stack and jumps to the call to setjmp), without calling any destructors. It was C anyway, so that didn't matter.

 

Structured exception handling in Win32 uses setjmp/longjmp (but hides them behind macros).

 

EDIT: My evil macro was basically something which went before a call to GetPadState, something like this in pseudocode

 

macro CHECK_EXIT_GAME:

 

if(select and start held down for 3 seconds) longjmp MainMenuHandler

else

 

end macro

 

;)

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites


It's pretty similar to using exceptions (it resets the stack and jumps to the call to setjmp)

Aye, but "control flow via exceptions" gives everyone the heebee jeebees.

 

call/cc only typically causes heart attacks in procedural programmers blink.png

0

Share this post


Link to post
Share on other sites

I was really quite surprised when I first attempted to add error checking at work, and my compilation failed with a [tt]error: use of undeclared label 'onError'[/tt].

 

From a ubiquitous global header in a (thankfully recently deceased) library within our project:

#define CheckSuccess(condition) do { \
    if (!condition) { \
        goto onError; \
    } \
} while (0) 

The idea that an error checking macro would require the current function to implement a specifically named label for error cleanup (or, indeed, that there would be significant error cleanup not handled by RAII), is a little unpleasant to me...

 

I should probably add that the author of this particular gem thought that they were working around the lack of exception handling on our target platform.

 

I used to do it simpler...

 

#define CheckPrecondition(condition)  {  if ( ! condition) goto  ON_PRECONDITION_FAIL; }

 

 

Could have sworn I used to get that to work in a (native code) FSM macro simplified script language

0

Share this post


Link to post
Share on other sites

 

I was really quite surprised when I first attempted to add error checking at work, and my compilation failed with a [tt]error: use of undeclared label 'onError'[/tt].

 

From a ubiquitous global header in a (thankfully recently deceased) library within our project:

#define CheckSuccess(condition) do { \
    if (!condition) { \
        goto onError; \
    } \
} while (0) 

The idea that an error checking macro would require the current function to implement a specifically named label for error cleanup (or, indeed, that there would be significant error cleanup not handled by RAII), is a little unpleasant to me...

 

I should probably add that the author of this particular gem thought that they were working around the lack of exception handling on our target platform.

 

I used to do it simpler...

 

#define CheckPrecondition(condition)  {  if ( ! condition) goto  ON_PRECONDITION_FAIL; }

 

 

Could have sworn I used to get that to work in a (native code) FSM macro simplified script language

 

It is simpler but also prone to errors that the do-while(false) method is isn't. The do-while(false) method requires a semicolon to work, while it is optional and can silently change the behavior of your code whether it is there or not with your variant.

 

But, I'd say that such hacks deserves some happy debugging times when they do change the behavior...

1

Share this post


Link to post
Share on other sites

http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
 
Not exactly goto in macro, but switch case is similar to goto.

Please refer to the gem at the bottom of that page:
* PuTTY is a Win32 Telnet and SSH client. The SSH protocol code contains real-life use of this coroutine trick.
  As far as I know, this is the worst piece of C hackery ever seen in serious production code.
0

Share this post


Link to post
Share on other sites

It is simpler but also prone to errors that the do-while(false) method is isn't. The do-while(false) method requires a semicolon to work, while it is optional and can silently change the behavior of your code whether it is there or not with your variant.

 

I used to do it simpler...

 

#define CheckPrecondition(condition)  {  if ( ! condition) goto  ON_PRECONDITION_FAIL; }

 

 

Could have sworn I used to get that to work in a (native code) FSM macro simplified script language

 

 

But, I'd say that such hacks deserves some happy debugging times when they do change the behavior...

 

It was used within an all 'scripted' code block setting (FSMs for object behaviors)  where the rest of the mini-language macros were used of similar definitions (and were not interwoven with normal code).  That language statement  defines actually would look more like  :

 

#define PRECOND(CONDX)  {  if ( ! CONDX) goto  STATEX##PRECOND_FAIL; }

 

The more interesting  defines were for the FSM state wrappers to work within  a BIG SWITCH.   and the ones needed to break each behavior state into 3 seperately run  behavioral phases of the game loop.  There could be  competing tasks for each object state,  which required seperated out phase processing (by tasks and individual target factors)  : first - discovery via state-task specialized sensor searches and situation conditions, then second - a phase prioritizing the search results , picking a 'best' task and executing the corresponding actions for the task,   third  -  a  epilog phase which did evaluations and state transitions based on results.  

 

Much of the overall complexity came from having interacting objects be frozen for equal evaluation, and after priority selection  both objects being locked into an interaction mode (locking out other object's interactions and forcing wait time for the duration of the action (related to the animation time).

 

The whole thing was organized to be table form programming (but was compiled into native code).  It had shortcomings of only being able to handle rudimentary inter-object interactions and signalling before becoming cumbersome.  I had to shift more complex object AI  operations to a Planner type scheme (easier to handle interupted tasks when you just abandon and reevaluate everything and restart).  The original mechanism was retained for objects with  simple reactionary and single minded behaviors as it was more lightweight and was still used on a majority of objects in the simulation.

 

 

The script code using such macro statments were not nested (eliminated most of any problematic cases) and quite regular in the way they were used (and so easy to spot any syntax mistakes).    So there were few problems of that type.

 

The debugging fun was actually in the intricacies of the inter-object behavior interactions with follow-on sequential states for many tasks (I was considering building a wizard to help streamline the creation of the script 'code' data to handle alot of the table building 'bookkeeping'....)

Edited by wodinoneeye
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0