为什么我不必为我的const变量创建malloc?

时间:2015-04-05 10:41:11

标签: c

我正在研究这个命令shell程序,我想知道为什么我在一个地方而不是其他地方使用malloc?我使用malloc作为tmp变量,为什么不使用其他变量?为什么一个变量需要动态内存而不需要其他变量?

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 个答案:

答案 0 :(得分:1)

由于其他指针指向常量值,因此编译器已将数据放入内存中。您无法更改它们或它们指向的数据(数据是文字的并且驻留在只读内存块中)。 tmp变量将指向内存的可变部分,因此您需要按原样分配它。

当然你可以静态地分配内存,这样你也不需要malloc,但动态分配就像名字所说的那样是动态的,所以你可以分配你需要定义的任何数量的运行时间,也不需要编译时间。与此情况类似,编译时不知道内存量。