如何清除用C这个数组指针? [英] How do I clear this array pointer in C?

查看:148
本文介绍了如何清除用C这个数组指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做与使用系统调用的基本bash的,但我有一个指针数组的一些小问题。

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.

要恢复我的code,我读了从标准输入与阅读()命令缓冲区,然后我用strsep()从参数和所有参数的命令分成数组。然后,我创建叉子(新工艺),并执行该命令与execvp相关参数()。

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. Then I create a new process with fork() and execute that command with the associated arguments with execvp().

这一切都进入一个无限循环,直到用户键入退出(而不是codeD还)。问题是,在第一次迭代后,我需要* pArgs是空的,下一个命令和参数。我不知道该怎么做...

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. And I don't know how to do it...

下面是我的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;
}

P.S:很抱歉,但我不能提供目前的输入和输出的测试用例。但愿,这不是很难理解和修复,你们并不需要它。以后我会后,如果它需要的,虽然...

P.S: Sorry but I can't provide an input and output test case at the moment. 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...

只是为了澄清一些事情:

我知道我问到如何清除数组,我得到了一个答案。但现在看来明显,我认为我的问题是不是那个意思,但该缓冲区正在收集由litb指出的垃圾。它更有意义来终止字符串的空字符,而不是清除阵列。这就是为什么我标志着litb的回答是正确的。

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. 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.

推荐答案

您的问题是,读取数据后,你不添加一个空字符。因此, strsep 通话不知道在哪里停下来。在C中,字符串必须以空字符(这就是所谓的终止空字符)终止。

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. In C, strings must be terminated by a null character (that's called the terminating null character).

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

通过在地方,我不认为现在应该清除什么阵,因为 execvp 将读取参数,直到第一个空指针。在不要循环,但补充说,空指针已经,这是由 strsep 在最后一次调用返回空指针

With that in place, i don't see what array should be cleared now, since execvp will read arguments until the first null pointer. The do loop, however, adds that null pointer already, which is the null pointer returned by the last invocation of strsep.

问题会当然也可以通过太刚清除bBuffer解决(数据,其中 * pArgs 指向第一个命令被扫描后)。需要注意的是你必须做的也扫描第一时间之前,因为你不能承担 bBuffer的字符数组初始化为任何合理的值。

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). 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.

memset(bBuffer, 0, sizeof bBuffer);

将刚在调用之前(但在任何情况下,只读最大限度 BUFSIZE-1 ,因为终止空字符必须具有的空间呢!)。

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!).

不过,正如我上面显示,你不需要这个memset的调用。只需手动添加终止空字符。

But as I've shown above, you don't need this memset call. Just add the terminating null character manually.

这篇关于如何清除用C这个数组指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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