Re: Array of pointer-to-functions
On 18/09/2012 16:06, Scott Lurndal wrote:
Juha Nieminen <nospam@thanks.invalid> writes:
Scott Lurndal <scott@slp53.sl.home> wrote:
Also prevents compile errors in the presence of goto's
You must be joking.
You conveniently omitted the context, was "declare where used".
$ g++ -o /tmp/bbb /tmp/a.cpp
/tmp/a.cpp: In function 'int main(int, char**)':
/tmp/a.cpp:27:1: error: jump to label 'leave' [-fpermissive]
/tmp/a.cpp:19:12: error: from here [-fpermissive]
/tmp/a.cpp:23:7: error: crosses initialization of 'int b'
There is no need of goto in your example to release the lock, hence
there is no need to define variables "at the beginning of the block"
(far from where they are used) to avoid the cross initialization. Using
the RAII idiom makes programs less error-prone, shorter and let you
initialize variables where they are used:
pthread_mutex_t lock;
int
main(int arg, char **argv)
{
int a = strtoul(argv[1], NULL, 0);
pthread_mutex_init(&lock, NULL);
struct Lock {
Lock() noexcept { pthread_mutex_lock(&lock); }
~Lock() { pthread_mutex_unlock(&lock); }
} lock;
a = a + 1;
if (a == 3)
return EXIT_SUCCESS;
a = a + 3;
int b = a;
printf("%d\n", b);
return EXIT_SUCCESS;
}