[Openmcl-devel] popen is not happy

Barry Perryman gekki_uk at hotmail.com
Wed Jun 4 21:57:38 UTC 2003

On 4/6/03 9:39 pm, "Erann Gat" <eg at flownet.com> wrote:

>> I'm less convinced that it's safe to pass stack-allocated strings to
>> #_popen; whether this works or not may depend on whether the forked
>> child is able to use them before the parent deallocates them.  It
>> looks very much like the exception you got here was in the child
>> process; typing an "X" at the prompt killed the child process, and
>> the pipe-based FILE* was returned to the parent process.
> That's a good theory, but there are two problems.  First, you'd have the
> same problem if you wrote the following C code:
> {
> char * cmd = "ls";
> char * mode = "r+";
> return popen(cmd, mode);
> }
> and if that were going to be a problem I would think there's be a pretty
> big warning in the popen man page.

That assumes that the C compiler allocates that "ls" and "r+" each time into
the code. It stores the string constants away, and only allocates the cmd
and mode pointers.

You can see this in the following quick hack that shows contents, pointers
and addresses of pointers to the string constants.


#include <stdio.h>

    char * s1 = "hello ";
    char * s2 = "world!";

    printf("%s%s\n", s1, s2);
    printf("s1 -> %d\t\t\ts2 ->%d\n", s1, s2);
    printf("&s1 -> %d\t\t&s2 ->%d\n", &s1, &s2);

    printf("Calling routine\n");

main(int argc, char **argv)
    routine();      /* should get the same pointers as previous call */
    routine2();     /* create additional stack frame, new pointers */
    return 0;

Openmcl-devel mailing list
Openmcl-devel at clozure.com

More information about the Openmcl-devel mailing list