简体   繁体   English

如何在C中清除此数组指针?

[英]How do I clear this array pointer in C?

I'm trying do to a basic bash with the use of system calls but I'm having some little problems with a pointer array. 我正在尝试使用系统调用进行基本的bash,但我对指针数组有一些小问题。

To resume my code, I read commands from the stdin with read() to a buffer then I use strsep() to separate the command from the arguments and all the arguments into an array. 为了恢复我的代码,我从stdin中读取带有read()的命令到缓冲区,然后我使用strsep()将命令与参数和所有参数分离到一个数组中。 Then I create a new process with fork() and execute that command with the associated arguments with execvp(). 然后我用fork()创建一个新进程,并使用execvp()的相关参数执行该命令。

All this goes into a infinite loop until the user types "quit" (not coded yet). 所有这一切都进入无限循环,直到用户键入“退出”(尚未编码)。 The problem is that after the first iteration, I need *pArgs to be empty, for the next command and arguments. 问题是在第一次迭代之后,我需要* pArgs为空,用于下一个命令和参数。 And I don't know how to do it... 我不知道怎么做......

Here's my code: 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
    char bBuffer[BUFSIZ], *pArgs[10], *aPtr = NULL, *sPtr;
    int aCount;
    pid_t pid;

    while(1) {
        write(1, "\e[1;31mmyBash \e[1;32m# \e[0m", 27);
        read(0, bBuffer, BUFSIZ);

        sPtr = bBuffer;
        aCount = 0;

        do {
            aPtr = strsep(&sPtr, " ");
            pArgs[aCount++] = aPtr;
        } while(aPtr);

        pArgs[aCount-2][strlen(pArgs[aCount-2])-1] = '\0';

        // Debug code to output pArgs content
        write(1, "|>", 2);
        write(1, pArgs[0], strlen(pArgs[0]));
        write(1, "<|", 2);

        if(strlen(pArgs[0]) > 1) {
            pid = fork();

            if(pid == -1) {
                perror("fork");
                exit(1);
            }

            if(pid == 0) {
                execvp(pArgs[0], pArgs);
                exit(0);
            }
        }
    }

    return 0;
}

PS: Sorry but I can't provide an input and output test case at the moment. PS:很抱歉,我目前无法提供输入和输出测试用例。 Hopefully, this is not that hard to understand and fix that you guys don't need it. 希望,这并不难理解和修复你们不需要它。 I'll post it later if it's needed though... 如果需要,我会稍后发布...

Just to clear things up: 只是为了清理:
I know I asked how to clear the array and I got an answer for that. 我知道我问过如何清除阵列,我得到了答案。 But it seems now obvious to me that my problem wasn't that, but the garbage that the buffer was collecting as pointed out by litb. 但现在我觉得很明显,我的问题不在于此,而是由litb指出缓冲区正在收集的垃圾。 It makes more sense to terminate the string with the null character than to clear the array. 使用空字符终止字符串比清除数组更有意义。 That's why I'm marking litb's answer as right one. 这就是为什么我将litb的答案标记为正确的答案。

int i;
for (i = 0; i < 10; i++)
   pArgs[i] = NULL;

Your problem is that you don't add a null character after the data read. 您的问题是在读取数据后不添加空字符。 So the strsep calls don't know where to stop. 所以strsep调用不知道在哪里停止。 In C, strings must be terminated by a null character (that's called the terminating null character). 在C中,字符串必须以空字符(称为终止空字符)终止。

// don't forget to add error handling at some point (s == -1)
ssize_t s = read(0, bBuffer, BUFSIZ-1);
bBuffer[s] = '\0';

With that in place, i don't see what array should be cleared now, since execvp will read arguments until the first null pointer. 有了这个,我不知道现在应该清除什么数组,因为execvp将读取参数,直到第一个空指针。 The do loop, however, adds that null pointer already, which is the null pointer returned by the last invocation of strsep . 然而, do循环已经添加了空指针,这是最后一次调用strsep返回的空指针。

The problem would of course also be solved too by just clearing bBuffer (the data where *pArgs is pointing to after the first command was scanned). 问题当然也可以通过清除bBuffer(扫描第一个命令后*pArgs指向的数据)来解决。 Note that you have to do that also before scanning the first time, since you can't assume the chars in bBuffer array are initialized to any sensible values. 请注意,您必须在第一次扫描之前执行此操作,因为您无法假设bBuffer数组中的字符被初始化为任何合理的值。

memset(bBuffer, 0, sizeof bBuffer);

Place that just before the read invocation (but in any case, read only maximally BUFSIZE-1 , because the terminating null character must have space too!). 放在read调用之前(但无论如何,最多只能读取BUFSIZE-1 ,因为终止空字符也必须有空格!)。

But as I've shown above, you don't need this memset call. 但正如我上面所示,你不需要这个memset调用。 Just add the terminating null character manually. 只需手动添加终止空字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM