使用execve时传递整数参数 [英] passing integer arguments when using execve

查看:217
本文介绍了使用execve时传递整数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用C系统调用进行编程的初学者。我正在尝试在我的一个程序中使用execve调用。我必须将一个整数作为参数传递给通过execve调用的程序。
但是,在互联网上阅读并看到示例代码,我可以看到我们只能将字符串作为参数传递。所以,我尝试使用'sprintf'将整数转换为字符串,然后使用'strcpy'将该字符串复制到必须通过execve传递的字符串数组的一个元素中。
但是使用strcpy会导致分段错误。我通过调试检查了这个,如果我不使用strcpy但只是写一些像 -
myargv [1] =123;
那么整个程序运行正常。
但是因为我必须传递一个变量整数作为参数而不是常量,所以我不能使用上面的方法。

I am a beginner at programming using system calls in C. I am trying to use the execve call in one of my programs. I have to pass an integer as an argument to the program that is being invoked through execve. However, reading on the internet and seeing sample code, I can see that we can pass only strings as arguments. So, I tried converting the integer to a string using 'sprintf' and then using 'strcpy' to copy that string into one of the elements of the array of strings that has to be passed through execve. But using strcpy leads to a segmentation fault. I checked this out by debugging and if I don't use strcpy but simply write something like - myargv[1]="123"; then the entire program works fine. But since I have to pass a variable integer as a parameter and not a constant, I cannot use the above method.

这个问题一直困扰着我一段时间请帮我解决一下我应该做些什么。

This problem has been bugging me for some time. Kindly help me out as to what I should do.

推荐答案

这几乎可以肯定是因为你没有为你的字符串分配空间。

It's almost certainly because you haven't allocated space for your string.

原因 myargv [1] =123; 有效是因为你将指针设置为指向a已存在的字符串(编译器通常将123放入预先初始化的内存中,然后才加载)。使用类似的东西:

The reason myargv[1]="123"; works is because you set the pointer to point to a string that already exists (the compiler generally puts that "123" into pre-initialised memory and it just gets loaded). By using something like:

char *myargv[10];
strcpy (myargv[1], "anything");

您通常会遇到问题,因为没有分配后备存储空间myargv [1] 指针。

you'll generally run into problems because there's no allocated backing storage for the myargv[1] pointer.

你可以做的只是设置 myargv [1] 直接到缓冲区,你 sprintf -ed字符串:

What you can do is to simply set myargv[1] directly to the buffer where you sprintf-ed the string:

char sprintf_buffer[whatever];
sprintf (sprintf_buffer, "%d", myInteger);
myargv[1] = sprintf_buffer;

或者,如果您正在重新使用该缓冲区(以便为多个整数覆盖该值)参数),使用 strdup

Or, if you're re-using that buffer (so that the value gets overwritten for multiple integer arguments), use strdup.

char sprintf_buffer[whatever];
sprintf (sprintf_buffer, "%d", myInteger1);
myargv[1] = strdup (sprintf_buffer);
sprintf (sprintf_buffer, "%d", myInteger2);
myargv[2] = strdup (sprintf_buffer);

如果出于某种原因,您的平台没有拥有 a strdup ,这里有一个给你:

And if, for some reason, your platform doesn't have a strdup, here's one for you:

char *strdup (const char *s) {
    char *d = (char *)(malloc (strlen (s) + 1));
    if (d != NULL)
        strcpy (d,s);
    return d;
}

请记住,您应该始终检查 strdup 以确保内存分配有效。我把它留在了这里,因为它与手头的问题无关。

Keep in mind you should always check the return value from strdup to make sure the memory allocation worked. I've left it out here since it's not relevant to the question at hand.

你可以避免多个内存分配缓冲区(如果你事先知道你需要的缓冲区的绝对数量):

You can avoid memory allocation with multiple buffers (provided you know the absolute number of buffers you'll need in advance):

char sprintf_buffer1[whatever];
char sprintf_buffer2[whatever];
sprintf (sprintf_buffer1, "%d", myInteger1);
myargv[1] = sprintf_buffer1;
sprintf (sprintf_buffer2, "%d", myInteger2);
myargv[2] = sprintf_buffer2;

这篇关于使用execve时传递整数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆