我这里有一个棘手的问题,我似乎无法弄清楚。当使用整个 if/else-if/else-if
时,如果 vector 中的参数不存在,它就会出现段错误。如果参数存在,那么它看起来运行良好。当我注释掉其中一个 else-if
block 时,它似乎也运行正常。
编辑:
void
priority_arg_create (gchar *arg)
{
gchar **argvp;
gint argcp;
gint i;
gint j;
argvp = get_arg_vector ();
argcp = 0;
i = 0;
/* obtain arg count not including the `NULL` terminator. */
while (argvp[argcp])
{
argcp++;
}
g_print ("From `rt_priority.c` line 42: %d\n", argcp);
/* Here we look for the jack arg `-Px` with the for loop then the if
statement. If it does not exist then we add it to arg vector with
`else/if` statement. */
for (i = 0; i <= argcp; i++)
{
if ((i == argcp -1) && (strncmp (argvp[i], "-P", 2) != 0))
{
g_print ("From `rt_priority.c` line 65\n");
/* Add space to arg vector for the jackd arg `-R`. */
argcp = argcp + 1;
/* If realtime arg exists then place priority arg right
after that.*/
if ((strncmp (argvp[1], "-r", 2) == 0) ||
(strncmp (argvp[1], "-R", 2) == 0))
{
/* Here we move the args over one to place `-Px` as the
third arg in the vector. */
for (j = argcp; j >= 2; j--)
{
argvp[j] = argvp[j - 1];
}
argvp[2] = arg;
}
else
{
/* Here we move the args over one to place `-Px` as the
second arg in the vector. */
for (j = argcp; j >= 1; j--)
{
argvp[j] = argvp[j - 1];
}
argvp[1] = arg;
}
break;
}
else if (g_strcmp0 (argvp[i], arg) == 0)
{
g_print ("From `rt_priority.c` line 51: %d\n", i);
break;
}
/* If `priority arg` is found but the number does not match
execute else/if statement. */
else if (strncmp (argvp[i], "-P", 2) == 0)
{
argvp[i] = arg;
break;
}
}
file_input (argvp, argcp);
}
这是 for ()
循环中 g_print ()
的输出
第 56 行
显示 argvp
,第 57 行
显示 argcp
:
From `rt_priority.c` line 42: 6
From `rt_priority.c` line 56: /usr/bin/jackd
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -dalsa
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -dhw:M2496
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -r48000
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -p128
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -n2
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: (null)
From `rt_priority.c` line 57: 6
Segmentation fault (core dumped)
编辑:
这是 get_arg_vector ()
函数:
gchar **argvp
get_arg_vector ()
{
gchar cmd[128];
gchar **argvp;
gchar *contents;
gint argcp;
gsize size;
/* Create path to file `.jackdrc` using `g_sprintf ()`. */
g_sprintf (cmd, "%s/.jackdrc", g_getenv ("HOME"));
/* Check if file path exists. */
if (g_file_test (cmd, G_FILE_TEST_EXISTS) == FALSE)
{
g_print ("File doesn't exist. Create file.");
return NULL;
}
g_file_get_contents (cmd, &contents, &size, NULL);
g_shell_parse_argv (contents, &argcp, &argvp, NULL);
return argvp;
}
最佳答案
最后一个 else-if 应该在 for 循环结束时调整 argvp 数组的大小。 if 和第一个 else-if 从 argvp 中读取当前值(可能为 0,这是一个错误)。可能代码一开始只包含最后一个 else-if,其他 if/else-ifs 是后来插入的。 更改 else-if 的顺序,使最后一个在其他之前运行,然后它应该可以工作。
要为 get_arg_vector 返回的数组中的一个额外元素腾出空间,只需重新分配它以包含另一个元素;即在返回 argvp 之前,在 get_arg_vector 的末尾,插入:
....
argvp = g_realloc (argvp, (argcp + 2) * sizeof *argvp);
return argvp;
}
数组通常包含 (argcp+1) 个元素,因此 (argcp+2) 使它大了一个元素。
关于c - 所有 `if/else-if/else-if` 语句的段错误,但在评论其中一个语句时没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911887/