[英]I'm writing a shell in C and I am having trouble with pointers to char arrays in a struct
When I run my code the first printParams()
call works perfectly.当我运行我的代码时,第一个
printParams()
调用完美运行。 But every call after fork()
the struct
loses all of its char
array values.但是在
fork()
之后的每次调用,该struct
都会丢失其所有char
数组值。 I'm not that knowledgeable of pointers, but I can tell that the root of this problem is probably pointer-based.我对指针不是很了解,但我可以看出这个问题的根源可能是基于指针的。 For example, the first
printParams()
will print out all of the values assigned in the Parse()
function.例如,第一个
printParams()
将打印出Parse()
函数中分配的所有值。 But after the fork()
, all of the integer values such as background
and argumentCount
are displayed but none of the string values associated with inputRedirect
or the string values held in the vectorArguments
array.但是在
fork()
之后,会显示所有整数值,例如background
和argumentCount
,但不会显示与inputRedirect
关联的字符串值或保存在vectorArguments
数组中的字符串值。
![Here is a photo of my output]] 1 ![这是我输出的照片]] 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "parse.h"
void printParams(Param_t * param);
struct PARAM
{
char *inputRedirect; /* file name or NULL */
char *outputRedirect; /* file name or NULL */
int background; /* ethier 0 (false) or 1 (true) */
int argumentCount; /* number of tokens in argument vector
*/
char *argumentVector[MAXARGS]; /* array of String */
};
typedef struct PARAM Param_t;
int main(int argc, char *argv[]){
int i;
int debug;
pid_t pid;
if(argc>1){
if(!strcmp(argv[1], "-debug"))
debug = 1;
}
Param_t * testParam = Parse();
if(debug == 1){
printParams(testParam);
}
pid = fork();
printParams(testParam);
if(pid == 0){
exit(1);
}
return 0;
}
void printParams(Param_t *param)
{
int i;
printf("InputRedirect: [%s]\n", (param->inputRedirect != NULL) ? param-
>inputRedirect: "NULL");
printf("OutputRedirect: [%s]\n", (param->outputRedirect != NULL) ?
param->outputRedirect: "NULL");
printf ("Background: [%d]\n", param->background);
printf ("ArgumentCount: [%d]\n", param->argumentCount);
for (i = 0; i < param->argumentCount; i++)
printf("ArgumentVector[%2d]: [%s]\n", i, param->argumentVector[i]);
}
Param_t* Parse(){
char *toke[MAXARGS];
int i = 0;
char str[MAXSTRLENGTH];
int j;
int k=0;
Param_t* testParam = malloc(sizeof(Param_t));
testParam->argumentCount = 0;
printf("Enter your commands:\n");
fgets(str, MAXSTRLENGTH, stdin);
toke[i] = strtok(str, " ");
//Tokenizes the user input into the toke array
while(toke[i] != NULL){
//printf("%s\n", toke[i]);
++i;
toke[i] = strtok(NULL, " ");
}
i=0;
char c;
while(toke[i] != NULL){
c = toke[i][0];
if(c == '<')
{
for(j=0; j<strlen(toke[i]); ++j ){
toke[i][j] = toke[i][j+1];
}
testParam->inputRedirect = toke[i];
}
else if(c == '>')
{
for(j=0; j<strlen(toke[i]); ++j ){
toke[i][j] = toke[i][j+1];
}
testParam->outputRedirect = toke[i];
}
else if(c == '&')
{
testParam->background = 1;
//background
}
else
{
testParam->argumentVector[k] = toke[i];
k++;
//save as cmd vector
}
++i;
}
testParam->argumentCount = k;
return testParam;
}
The reason you loose all char *
values is because strtok()
function doesn't create buffer.丢失所有
char *
值的原因是strtok()
函数不创建缓冲区。 Basically all your char*
consist of an address pointing into str
variable you read with fgets()
.基本上,您的所有
char*
都包含一个指向您使用fgets()
读取的str
变量的地址。 The str
variable has scope only to end of Parse()
function. str
变量的作用域仅限于Parse()
函数的末尾。
Solution for this:解决方案:
Replace:代替:
testParam->inputRedirect = toke[i];
with:和:
testParam->inputRedirect = malloc(MAXSTRLENGTH);
memset( testParam->inputRedirect, 0, MAXSTRLENGTH);
memcpy( testParam->inputRedirect, toke[i], strlen(toke[i]) );
But please note, that this leads to memory leak since there is no free()
.但请注意,这会导致内存泄漏,因为没有
free()
。
Sugestion:建议:
Create static instance of structure in main
and give its pointer to Parse
function.在
main
中创建结构的静态实例并将其指针指向Parse
函数。
Param_t testParam;
Parse( &testParam );
Let the Parse
function fill it.让
Parse
函数填充它。 And at the end of main
call free for all char *
buffers inside testParam
并且在
testParam
中的所有char *
缓冲区的main
调用结束时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.