execv函数参数构建

zhongxiangchen 2018-07-10 01:32:52
我需要使用execv调用一个二进制程序,需要从一个配置文件中读取多个参数传递给二进制程序而且参数数量不确定,请问要怎么构建一个char **argv的参数呢?我现在的想法是从配置文件中每读取到一个参数就malloc一下然后赋值地址给自定义的 char **argv元素。请问这是正确的方法吗?新手求指教。
...全文
270 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongxiangchen 2018-07-10
  • 打赏
  • 举报
回复
引用 3 楼 赵4老师的回复:
别忘了还有realloc
多谢赵4.....er老师提醒
赵4老师 2018-07-10
  • 打赏
  • 举报
回复
别忘了还有realloc
zhongxiangchen 2018-07-10
  • 打赏
  • 举报
回复
多谢您的回复,man手册我读过没有提到过execv系列函数的argv数组指针的构建。您给的这个是execl系列函数的参数介绍,这个其实比较简单我觉得....。
如果使用我的不断malloc的想法的话,释放内存肯定是个问题。感觉这样做太机械化了。
sghcpt 2018-07-10
  • 打赏
  • 举报
回复
楼主:linux man page说明的参数如下:
The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

按照你说的,你的方法应该可以的,尝试实践才知道结果。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧