NO

Author Topic: Computed goto?  (Read 201 times)

Offline Akko

  • Member
  • *
  • Posts: 12
Computed goto?
« on: November 15, 2018, 04:41:38 pm »
In gcc you can get the address of a label defined in the current function (or a containing function) with the unary operator ‘&&’. The value has type void *. This value is a constant and can be used wherever a constant of that type is valid. For example:

void *ptr;
/* … */
foo:       // label
/* … */
ptr = &&foo;


To use these values, you need to be able to jump to one. This is done with the computed goto Statement: goto *exp;.
For example,

goto *ptr;

How to do this in Pelles C ???

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1673
Re: Computed goto?
« Reply #1 on: November 18, 2018, 08:36:55 pm »
This is a GCC extension, and is not part of C standard.
PellesC doesn't implement this extension.
To create jump tables you must use standard switch statement.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 760
Re: Computed goto?
« Reply #2 on: December 02, 2018, 08:38:13 am »
Thanks for giving me one more reason to hate GCC...

Ralf

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1673
Re: Computed goto?
« Reply #3 on: December 02, 2018, 11:33:26 am »
This feature make sense only when used for OS kernel modules, where the coder can build the equivalent of an assembler jump-table. And it should allow even the implementation of inter-routines jumps, known as coroutines.
In fact while the documentation says that the programmer should avoid to use labels defined in different routines in local jumps, don't explicitly states that they are blocked by the compiler.
The advantage over the standard switch statement is that it is someway invisible to the optimizations, and allows more control over code for the programmer.
I don't know how much it is used in the Linux kernel, but if it's there it should be of some use  ???
Anyway for standard programming it must be avoided (in GCC obviously, the other compilers don't have it!). An inexpert programmer can create jumps between incompatible functions call (i.e. different number of parameters) leading to stack or, more generally, memory corruption and consequential program crash.
Inter-functions jumps are still permitted using the standard int setjmp(jmp_buf env)/void longjmp(jmp_buf env, int value) functions from setjmp.h.