[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: mktemp segfaults

Bernhard Rieder (e9325898@student.tuwien.ac.at) wrote:

> test.c: -----------------------------------
> #include <stdlib.h>
> char *template = "/tmp/tmpfileXXXXXX";
> int main () {
>    return printf("%s\n", mktemp(template));
> }
> -------------------------------------------

As others have noted, you cannot pass a string constant to mktemp().

Use this instead:

  char template[] = "/tmp/tmpfileXXXXXX";

Using the brackets ("[]") here forces the allocation of an array big
enough to hold the initializer value ("/tmp/tmpfileXXXXXX" with trailing
NUL character).  With the "char *" you're only allocating a pointer and
telling it to point to a string constant (which is in read-only memory).

Greg Wooledge                    | Distributed.NET http://www.distributed.net/
wooledge@kellnet.com             | because a CPU is a terrible thing to waste.
http://www.kellnet.com/wooledge/ |

Reply to: