- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个使用 getopt
函数的简单程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char *fname;
int c;
printf("Before getopt: optind is %d, address is %p \n", optind, &optind);
while ((c = getopt(argc, argv, "f:")) != -1)
switch (c) {
case 'f':
fname = optarg;
break;
}
printf("After getopt: optind is %d, address is %p \n", optind, &optind);
return 0;
}
执行它,输出:
# ./test -f 1
Before getopt: optind is 1, address is 0x601040
After getopt: optind is 3, address is 0x601040
但是在使用gdb
调试时,出现了一些奇怪的事情:
13 printf("Before getopt: optind is %d, address is %p \n", optind, &optind);
(gdb) n
Before getopt: optind is 1, address is 0x601040
15 while ((c = getopt(argc, argv, "f:")) != -1)
(gdb) p &optind
$1 = (int *) 0x7ffff7dd42a0 <optind>
(gdb) n
16 switch (c) {
(gdb) n
18 fname = optarg;
(gdb)
19 break;
(gdb) p &optind
$2 = (int *) 0x7ffff7dd42a0 <optind>
(gdb) p optind
$3 = 1
我可以看到使用p optind
命令,它输出1
(应该是3
),这个变量地址是0x7ffff7dd42a0
,而不是 0x601040
。
使用readelf
命令:
# readelf -a test | grep optind
000000601040 000600000005 R_X86_64_COPY 0000000000601040 optind + 0
6: 0000000000601040 4 OBJECT GLOBAL DEFAULT 25 optind@GLIBC_2.2.5 (2)
54: 0000000000601040 4 OBJECT GLOBAL DEFAULT 25 optind@@GLIBC_2.2.5
它还显示一个optind
,它的地址应该是0x601040
。那么在使用gdb
时,为什么会从0x7ffff7dd42a0
获取optind
呢?这是什么?
更新:
使用最新的gdb 7.11
,发现这个问题已经修复。
最佳答案
这是一个 obscure gdb bug ,由另一个称为复制重定位的模糊功能触发。
关于c - 为什么 gdb 得到错误的 "optind"变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787697/
这个问题已经有答案了: what's the difference between the printf and vprintf function families, and when should
我提出这个问题是因为没有太多关于如何为每个循环分配这个 optind。 Man page说: The variable optind is the index of the next element
我一直在学习命令行参数解析。关于这个已经有很长的线索了,我不想在这里挑起一个: Using getopts in bash shell script to get long and short com
根据我将选项传递给该程序的格式,变量 optind 将正确递增以反射(reflect)下一个选项值,否则不会递增。 如果我使用 ./cfind -aru,我会得到以下(奇怪的)输出。 optind:
我正在编写一个使用 getopt 函数的简单程序: #include #include #include int main(int argc, char **argv) { ch
我的shell脚本很简单,如下: while getopts "abc:" flag; do echo "$flag" $OPTIND $OPTARG done 我做了如下测
我意识到 shift 将 cli args 数组向左移动 n 个空间,n 的默认值为 1。这意味着我可以在 while 循环内使用 $1 shift 将数组的值分配给现有变量。我不太明白的是为什么在下
$OPTIND 在POSIX shell 中的定义,bash 是相当一致和直观的——它是下一个要读取的arg 的索引。然而,它在 zsh 中的行为非常令人费解,我找不到它的文档。 例子: # ./te
我是一名优秀的程序员,十分优秀!