繁体   English   中英

二维char **数组中的strcpy()分段错误

[英]strcpy() segmentation fault in 2-dimensional char** array

我正在编写一个shell来抓取输入并将其存储在字符串(char *)数组中。 要启用像流水线操作这样的UNIX操作,我希望能够编写像

echo this | function more | function2

为此,我在while循环中收集命令及其参数,并尝试将参数数组的所有内容复制到一个新的二维数组中,该数组包含整个命令行,例如“ ls -f -a ”。 到目前为止,这是有效的,但是,然后,我试图在命令行中找到“ | ”时重置循环,忽略“ | ”,并开始执行相同的新循环,直到'\\0'找到了,这标志着原始输入的结束:

char* arguments[MAXARGS]; // holds argument components, e.g. "ls" or "-f"
char** arg_collection[MAXARGS][MAXARGS]; // supposed to hold command lines, e.g. "ls -f -a"
argc_current = 0; // iterator for the loop, counts arguments
col_iterator = 0; // iterator for amount of command lines
int sentinel = 1; // sentinel value for while loop
...

while(sentinel)
{
    arguments[argc_current] = (char*) malloc (sizeof(word)); // word is defined, just not listed on here - refers to the currently read command / argument
     strcpy(arguments[argc_current], word); // copy current word into arguments

    if(tokens[argc_current] == TOKEN_TERMINATOR || tokens[argc_curernt] == TOKEN_NULL)
    {
       sentinel = 0; // tokens holds information about the type of word, e.g. if it is '\0'
    }

    if(tokens[argc_current] == T_BAR) // if the word is "|"
    {
       for(i = 0; i < argc_current; i++)
       {
          strcpy(arg_collection[col_iterator][i], arguments[i]); // copy argument list into collection
       }

       col_iterator++; // increment command line counter
       argc_current = 0; // reset argument counter, restart the loop
    }

    else
    {
        arg_current++; // increment current argument counter
    }
}

evaluating arguments here..

第一个循环工作正常,例如,如果我键入

echo this | echo more |

它填充arg_collection[0][0]arg_collection[0][1] ,因此我得到“ echo this ”。 然而,在将col_iterator增加到1之后,在注意到第二个“|”时调用strcpy时,我以分段错误的形式撞到了一堵砖墙。 为什么?

它看起来像是一个word char* 如果是这样,你应该改变

arguments[argc_current] = (char*) malloc (sizeof(word));

arguments[argc_current] = malloc(strlen(word)+1);

sizeof(word)将给出指针变量的大小,而不是它指向的数据的大小。 strlen告诉你word的字符数。 '\\0'终止符需要+1

首先,您应该在使用之前分配arg_collection。 (因为你没有提供整个代码,我假设你不这样做)。

然后你在线上取消引用你的arg_collection两次:

strcpy(arg_collection[col_iterator][i], arguments[i]); // copy argument list into collection

当你必须的时候

strcpy(arg_collection[col_iterator], arguments[i]); 

也就是说,当它只是一个char时,你将arg_collection中string col_iterator的索引i处的char视为char *。

暂无
暂无

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

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