H
hifrnds007
how the function poiners avoids the usage of switch cases?
how the function poiners avoids the usage of switch cases?
1 The controlling expression of a switch statement shall have integer type.
6.8.4.2 of n1124.pdf (C99 draft)
Function pointers are pointers and not integers.
how the function poiners avoids the usage of switch cases?
how the function poiners avoids the usage of switch cases?
how the function poiners avoids the usage of switch cases?
Tomás Ó hÉilidhe said:Here's a typical switch statement:
switch (user_input)
{
case 0: DeleteFile(); break;
case 1: CopyFile(); break;
case 2: RenameFile(); break;
case 4: MoveFile();
}
And here's the function pointer equivalent:
void (*const funcs[4])(void) = {DeleteFile,CopyFile,Renamefile,MoveFile};
funcs[user_input];
Michal Nazarewicz said:Forgot about "()" and to be precise you should check if user_input is
May I suggest that's a very dubious practice.Tom�������������������������������� said:The only redudant checks I use in my programs are asserts.
May I suggest that's a very dubious practice.
Debug build crashes predictably; Release build (with NDEBUG #define'd)
either crashes unpredictably or wanders away mysteriously.
Use asserts for debugging and never for user input. On the API level, your
user can be another programmer.
- Ark
Why not assert(0), so you get the file and line without having to use acr88192 said:there are different ways to invoke crashes, but the one I typically use is
this:
*(int *)-1=-1;
I pity those poor windows developerson linux, this option works better though, since linux crashes give a
general debugger-usable core dump (sadly absent on windows...).
Ian Collins said:Why not assert(0), so you get the file and line without having to use a
debugger?
I pity those poor windows developers![]()
May I suggest that's a very dubious practice.
Debug build crashes predictably; Release build (with NDEBUG #define'd)
either crashes unpredictably or wanders away mysteriously.
Ian said:cr88192 wrote:
.... snip ...
I pity those poor windows developers![]()
One should never use assert for bad external inputs (inputs to thecr88192 said:file and line, by themselves, are not often all that helpful (they are
better than nothing, but when something crashes, usually a lot more is
helpful).
often the exact state at the time of crash (local variables, globals, stack
parameters, ...) are very helpful in identifying a bug.
also, usually the kinds of problems that need this, are a lot more involved
than the simple "bad input" type problems assert is good at pointing out
(more often, it is along the lines of "something has gone fundamentally
wrong here").
A decent debugger will stop on abort. Failing that, just set a breakpoint.so, neither assert nor abort will give all this. it is "too clean", the
debugger just thinks the program exited with an error code, which does not
allow much examination of the program state at the point of the crash.
and don't forget not to quote signatures!sadly, I develop primarily on windows, but I guess, technically, I could
still be regarded as a cross-platform developer (I try to write code that
builds on both windows and linux, and test things on linux every once in a
great while...).
Try going the other way...
May I suggest that's a very dubious practice.
Tomás Ó hÉilidhe said:The only redudant checks I use in my programs are asserts.
cr88192 said:there are different ways to invoke crashes, but the one I typically use is
this:
*(int *)-1=-1;
Michal Nazarewicz said:How about:
#define CRASH do { \
fprintf(stderr, "%s: %d: going to crash\n", __FILE__, __LINE__); \
*(int*)-1 = 42; \
} while (0)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.