我有一个棘手的问题,我似乎无法弄明白。当使用整个if/else-if/else-if
时,如果向量中的参数不存在,则会出现段错误。如果参数存在,那么它似乎运行正常。当我注释掉其中一个else-if
块时,它似乎也运行正常。
编辑:
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);
}
这是g_print ()
循环中for ()
的输出
line 56
显示argvp
,line 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;
}
答案 0 :(得分:0)
此:
for (i = 0; i <= argcp + 1; i++)
很吓人。我们知道argv[argcp]
是NULL
,来自进行搜索的循环。因此,使用<=
和 + 1
,此循环超过了两个。
循环时打印当前索引,并打印argcp
。
答案 1 :(得分:0)
最后 else-if 应该在 -loop中到达结尾时调整 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)使其成为一个更大的元素。