函数指针不指向任何东西——如何避免这种情况?
Function pointer points to nothing — how do I avoid this?
我目前有一个 typedef 指针函数,它不指向任何导致分段错误(核心转储)的东西。我正在想办法避免它,但目前还想不出。
我的代码:
typedef void (*MenuFunction)(System*);
int main(int argc, char ** argv)
{
...
/* While loop for my menu */
while(1)
{
printf("Main Menu\n");
printf("%s\n", menu[0].text);
printf("%s\n", menu[1].text);
printf("%s\n", menu[2].text);
printf("Select your option (1-3): ");
(*getMenuChoice(menu))(&system);
}
}
/* Function that points to the menu function */
MenuFunction getMenuChoice(MenuItem * menu)
{
MenuFunction function = NULL;
char select[50];
fgets(select, 50, stdin);
if(select[strlen(select)-1] == '\n')
{
switch(select[0])
{
case '1':
function = menu[0].function;
break;
case '2':
function = menu[1].function;
break;
case '3':
function = menu[2].function;
exit(0);
break;
default:
printf("Invalid option\n");
}
}
else
{
readRestOfLine();
printf("Error: buffer overflow. Please try again, entering less data");
}
return function;
}
编辑:
现在,我想出的解决方案是创建一个没有任何内容的函数,这样我就可以让函数指向某些东西。我不认为这是理想的,但它会在短期内做到 运行。
void skip()
{ }
为了避免'this'段故障事件:
在通过 typedef 实例调用函数之前,检查 typedef 实例是否包含 NULL 以外的内容
管理所有指针(数据函数和函数指针)的一个很好的策略是:让它们始终指向有效的东西,或者为空。即:
- 当你声明一个指针变量时,总是初始化它,要么指向有效的东西,要么指向 NULL。
- 在使用指针之前,确保它不是 NULL。
- 当您执行任何会使指针无效的操作时,请将其设置回 NULL。
在您的情况下,您在初始化时已经遵循了规则 1
MenuFunction function = NULL;
因此,如果选择无效,您的 getMenuChoice()
函数 returns NULL。没什么问题,这是一种常见的模式。
你还需要做的是遵守规则 2。现在你有
(*getMenuChoice(menu))(&system);
有点啰嗦:你调用 getMenuChoice()
,然后立即调用它 return 指向的函数。将其分成几行:
MenuFunction fp;
fp = getMenuChoice(menu);
if(fp == NULL)
fprintf(stderr, "invalid choice\n");
else
(*fp)(&system);
在这里,我们在一个新变量fp
中捕获getMenuChoice
的return值,我们在调用它之前测试以确保它不为NULL。
[P.S。在你的情况下,你不需要担心我的规则 3。]
我目前有一个 typedef 指针函数,它不指向任何导致分段错误(核心转储)的东西。我正在想办法避免它,但目前还想不出。
我的代码:
typedef void (*MenuFunction)(System*);
int main(int argc, char ** argv)
{
...
/* While loop for my menu */
while(1)
{
printf("Main Menu\n");
printf("%s\n", menu[0].text);
printf("%s\n", menu[1].text);
printf("%s\n", menu[2].text);
printf("Select your option (1-3): ");
(*getMenuChoice(menu))(&system);
}
}
/* Function that points to the menu function */
MenuFunction getMenuChoice(MenuItem * menu)
{
MenuFunction function = NULL;
char select[50];
fgets(select, 50, stdin);
if(select[strlen(select)-1] == '\n')
{
switch(select[0])
{
case '1':
function = menu[0].function;
break;
case '2':
function = menu[1].function;
break;
case '3':
function = menu[2].function;
exit(0);
break;
default:
printf("Invalid option\n");
}
}
else
{
readRestOfLine();
printf("Error: buffer overflow. Please try again, entering less data");
}
return function;
}
编辑:
现在,我想出的解决方案是创建一个没有任何内容的函数,这样我就可以让函数指向某些东西。我不认为这是理想的,但它会在短期内做到 运行。
void skip()
{ }
为了避免'this'段故障事件:
在通过 typedef 实例调用函数之前,检查 typedef 实例是否包含 NULL 以外的内容
管理所有指针(数据函数和函数指针)的一个很好的策略是:让它们始终指向有效的东西,或者为空。即:
- 当你声明一个指针变量时,总是初始化它,要么指向有效的东西,要么指向 NULL。
- 在使用指针之前,确保它不是 NULL。
- 当您执行任何会使指针无效的操作时,请将其设置回 NULL。
在您的情况下,您在初始化时已经遵循了规则 1
MenuFunction function = NULL;
因此,如果选择无效,您的 getMenuChoice()
函数 returns NULL。没什么问题,这是一种常见的模式。
你还需要做的是遵守规则 2。现在你有
(*getMenuChoice(menu))(&system);
有点啰嗦:你调用 getMenuChoice()
,然后立即调用它 return 指向的函数。将其分成几行:
MenuFunction fp;
fp = getMenuChoice(menu);
if(fp == NULL)
fprintf(stderr, "invalid choice\n");
else
(*fp)(&system);
在这里,我们在一个新变量fp
中捕获getMenuChoice
的return值,我们在调用它之前测试以确保它不为NULL。
[P.S。在你的情况下,你不需要担心我的规则 3。]