这些线
case 'c': cvalue = optarg; break;
看起来很怪异,因为optarg的内容不会被复制到cvalue中,它们只是复制指针.但它有效:
$testopt -a -b -c fooaflag = 1,bflag = 1,cvalue = foo
我希望通过第二次调用getopt()来覆盖optarg,所以我根据这个例子编写了我的own program.令人惊讶的是,optarg不会被覆盖.
$testopt -p -f me -t youpflag = 1,from = me,to = you
这是否一致,还是应该总是复制/复制?
我是否必须照顾optarg中返回的所有内容?
我真的很幸运,optarg的realloc()不会分配到同一个地址吗?
If the option has an argument,getopt returns the argument by storing it in the variable optarg. You don’t ordinarily need to copy the optarg string,since it is a pointer into the original argv array,not into a static area that might be overwritten.
这就是为什么它不需要复制或分配. POSIX documentation需要这个optarg.
总结以上是内存溢出为你收集整理的c – 为什么`optarg`没有被覆盖?全部内容,希望文章能够帮你解决c – 为什么`optarg`没有被覆盖?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)