[英]returning a pointer to a struct which has a char ** pointer-to-pointer in it
I just cant understand, how am I supposed to return a struct which contains a double pointer in it.... when ever i try it prints correct values in same function... but prints garbage values when i print after i return it to main...我只是不明白,我应该如何返回一个包含双指针的结构......当我尝试在同一个函数中打印正确的值时......主要的...
struct arguments{
char **argv;
int argc;
};
this is the struct.这是结构。
struct arguments * shell_make_argu(char *line)
{
struct arguments *data=malloc(sizeof(struct arguments));
int bufsize = word_BUFSIZE , i;
data->argc = 0;
data->argv = malloc(bufsize * sizeof(char*));
char *word;
if (!data->argv) {
fprintf(stderr, "shell memory allocation error\n");
exit(EXIT_FAILURE);
}
word = strtok(line, word_separater);
while (word != NULL) {
data->argv[data->argc] = word;
data->argc++;
if (data->argc >= bufsize) {
bufsize += word_BUFSIZE;
data->argv = realloc(data->argv, bufsize * sizeof(char*));
if (!data->argv) {
fprintf(stderr, "shell memory allocation error\n");
exit(EXIT_FAILURE);
}
}
word = strtok(NULL, word_separater);
}
data->argv[data->argc] = NULL;
for(i=0;i<data->argc;i++)
printf("Command was: %s\n",data->argv[i]);
return data;
}
and this is how i am receiving the struct in main....这就是我在 main 中接收结构的方式....
int main(int argc,char **argv)
{
struct arguments *data = malloc(sizeof(struct arguments));
char *line;
int status = 1 , i;
do {
printf("T_Shell>> ");
line = shell_take_input();
if(line != '\0' && strcmp(line,"found ctrl_D") != 0){
data = shell_make_argu(line);
free(line);
for(i=0;i<data->argc;i++)
printf("Command was: %s\n",data->argv[i]);
shell_execute_comand(data->argc,data->argv);
}
else if(line == '\0')
printf("\n");
else
break;
} while (status);
system("clear");
return 0;
}
You did free
to line
before printing data->argv
in main
function, which is bad because each elements of data->argv
are pointing to the buffer pointed by line
.你做
free
到line
打印之前data->argv
在main
函数,因为每个元素是坏的data->argv
指向的缓冲区指向的line
。
Do the free
after calling shell_execute_comand
, or allocate new memory and copy the strings returned by strtok
instead of assigning strtok
's return value directly to the elements of data->argv
.在调用
shell_execute_comand
之后做free
,或者分配新内存并复制strtok
返回的字符串,而不是将strtok
的返回值直接分配给data->argv
的元素。
shell_make_argu() function: shell_make_argu() 函数:
struct arguments* shell_make_argu(char *line)
{
struct arguments *data = malloc(sizeof(struct arguments));
int bufsize = word_BUFSIZE , i;
data->argc = 0;
data->argv = malloc(bufsize * sizeof(char*));
char *word;
if (!data->argv)
{
fprintf(stderr, "shell memory allocation error\n");
exit(EXIT_FAILURE);
}
word = strtok(line, word_separater);
while (word != NULL)
{
if (data->argc == bufsize) <-- Moved realloc at beginning of loop
{
bufsize += word_BUFSIZE;
data->argv = realloc(data->argv, bufsize * sizeof(char*));
if (!data->argv)
{
fprintf(stderr, "shell memory allocation error\n");
exit(EXIT_FAILURE);
}
}
data->argv[data->argc] = malloc(strlen(word)+1); <-- Added allocation of argv[argc] char pointer
strcpy(data->argv[data->argc], word); <-- Added storing token
data->argc++;
word = strtok(NULL, word_separater);
}
//data->argv[data->argc] = NULL; <-- Not required, can be removed
for(i=0;i<data->argc;i++)
printf("Command was: %s\n",data->argv[i]);
return data;
}
main() function:主功能:
int main(int argc, char **argv)
{
struct arguments *data; <-- Removed malloc, make_shell_argu() returns allocated pointer
char *line;
int status = 1 , i;
do
{
printf("T_Shell>> ");
line = shell_take_input();
if(line != NULL && strcmp(line,"found ctrl_D") != 0) <-- Changed condition check with (line != NULL)
{
data = shell_make_argu(line);
free(line);
for(i=0;i<data->argc;i++)
printf("Command was: %s\n",data->argv[i]);
shell_execute_comand(data->argc,data->argv);
for(i=0;i<data->argc;i++) <-- Added memory releasing code
{
free(data->argv[i]);
}
free(data->argv);
free(data); <-- Freeing 'data' also, make_shell_argu() will return new allocated pointer next time
}
else if(line == NULL) <-- Changed condition check to (line == NULL)
{
printf("\n");
}
else
{
status = 0;
}
} while (status);
system("clear");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.