2015-04-05 52 views
0

我正在处理这个命令shell程序,我想知道为什么我在一个地方使用malloc而不是其他地方?我为tmp变量使用malloc,为什么不为其他变量?为什么一个变量需要动态记忆而不是其他变量?为什么我不需要为const变量创建malloc?

struct command 
{ 
    const char **argv; 
}; 

int 
spawn_proc (int in, int out, struct command *cmd) 
{ 
    pid_t pid; 

    if ((pid = fork()) == 0) 
    { 
     if (in != 0) 
     { 
      dup2 (in, 0); 
      close (in); 
     } 

     if (out != 1) 
     { 
      dup2 (out, 1); 
      close (out); 
     } 

     return execvp (cmd->argv [0], (char * const *)cmd->argv); 
    } 

    return pid; 
} 

int 
fork_pipes (int n, struct command *cmd) 
{ 
    int i; 
    pid_t pid; 
    int in, fd [2]; 

    /* The first process should get its input from the original file descriptor 0. */ 
    in = 0; 

    /* Note the loop bound, we spawn here all, but the last stage of the pipeline. */ 
    for (i = 0; i < n - 1; ++i) 
    { 
     pipe (fd); 

     /* f [1] is the write end of the pipe, we carry `in` from the prev iteration. */ 
     spawn_proc (in, fd [1], cmd + i); 

     /* No need for the write and of the pipe, the child will write here. */ 
     close (fd [1]); 

     /* Keep the read end of the pipe, the next child will read from there. */ 
     in = fd [0]; 
    } 

    /* Last stage of the pipeline - set stdin be the read end of the previous pipe 
     and output to the original file descriptor 1. */ 
    if (in != 0) 
     dup2 (in, 0); 

    /* Execute the last stage with the current process. */ 
    return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv); 
} 

int 
main (int argc, char ** argv) 
{ 
    printf("in main..."); 
    int i; 

    if (argc == 1) { 
     const char *printenv[] = { "printenv", 0}; 
     const char *sort[] = { "sort", 0 }; 
     const char *less[] = { "less", 0 }; 

     struct command cmd [] = { {printenv}, {sort}, {less} }; 
     return fork_pipes (3, cmd); 
    } 
    if (argc > 1) { 
     char *tmp; 

    // Compute required buffer length 
    int len = 1; // adds 1 to the length to account for the \0 terminating char 
    for(i=1; i<argc; i++) 
    { 
     len += strlen(argv[i]) + 2; // +2 accounts for length of "\\|" 
    } 

    // Allocate buffer 
    tmp = (char*) malloc(len); 
    tmp[0] = '\0'; 
    // Concatenate argument into buffer 
    int pos = 0; 
    for(i=1; i<argc; i++) 
    { 
     pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]); 
    } 

    printf("tmp:%s", tmp); 
    fflush(stdout); // force string to be printed 

     const char *printenv[] = { "printenv", 0}; 
     const char *grep[] = { "grep", "-E", tmp, NULL}; 

     const char *sort[] = { "sort", 0 }; 
     const char *less[] = { "less", 0 }; 

     struct command cmd [] = { {printenv}, {grep}, {sort}, {less} }; 
     return fork_pipes (4, cmd); 
     free(tmp); 
    } 
} 
+1

您指的是哪些变量? – juanchopanza 2015-04-05 12:03:27

回答

1

由于其他指针指向常量值,数据已被编译器放入内存。您不能更改它们或它们指向的数据(数据是文字并位于只读内存块中)。 tmp变量将指向内存的一个可变部分,因此您需要分配它。

当然,您可以静态分配内存,以便您不需要malloc,但动态分配是,如名称所示,动态,因此您可以分配您需要的任何数量定义的运行时间和编译时间。就像在这种情况下一样,编译时内存的数量是未知的。

相关问题