tmpnam问题 [英] tmpnam question

查看:48
本文介绍了tmpnam问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




char buf [FILENAME_MAX];

strcpy(buf,tmpnam(NULL));
之间有什么区别?



char buf [FILENAME_MAX];

tmpnam(buf);


根据我的文档都调用写入内部静态缓冲区。然后使用参数(或内部静态缓冲区)的内容是什么?

解决方案

在comp中。 lang.ci阅读:

有什么区别

char buf [FILENAME_MAX];
strcpy(buf,tmpnam(NULL) );


char buf [FILENAME_MAX];
tmpnam(buf);

根据我的文档,两个调用都写入内部静态缓冲区。什么是参数的使用(或内部静态缓冲区)?




认为它可以节省你的strcpy。这可以很方便,因为tmpnam允许失败,在这种情况下它会返回一个空指针,这使得你的第一个例子有未定义的行为。


,标准也没有说必须使用内部静态缓冲区
如果提供了非空指针则为
。事实上它暗示这样的缓冲需要

不能使用。


-

a签名


" Serve Laurijssen" < cs@nospam.comp.com>写道:

有什么区别

char buf [FILENAME_MAX];
strcpy(buf,tmpnam(NULL));


char buf [FILENAME_MAX];
tmpnam(buf);


实际上没什么。


但是,请注意,对于tmpnam()生成的文件名,你应该使用

L_tmpnam,而不是FILENAME_MAX。

根据我的文档,两个调用都写入内部静态缓冲区。然后使用参数(或内部静态缓冲区)是什么?




您的文档是错误的。只有第一个调用写入静态缓冲区;

然后手动将其复制到您自己的缓冲区。第二个调用将
直接写入缓冲区。在两种情况下,最终结果是

文件名最终在缓冲区中;第二种情况很可能,虽然不是保证,但保证稍微快一点。


Richard


Serve Laurijssen写道:

有什么区别

char buf [FILENAME_MAX];
strcpy(buf,tmpnam(NULL) );


char buf [FILENAME_MAX];
tmpnam(buf);

根据我的文档,两个调用都写入内部静态缓冲区。然后使用参数(或内部静态缓冲区)是什么?



第一个表格


char * p = tmpnam(NULL);


创建一个不是现有文件名称的字符串,并返回一个指向内部静态数组的

指针包含字符串。


第二种形式


char s [L_tmpnam];

char * p = tmpnam (s);


将字符串存储在s中并返回指向字符数组的指针

" s []"。换句话说,p表示p。将指向s [],而不是指向

内部静态数组。


内部静态缓冲区在使用时不会发挥任何可观察的作用

第二种形式。


有些程序员可能会认为第一种形式更方便,因为

没有必要定义

之前调用tmpnam(NULL)之前的字符数组。


您可以通过运行以下程序检查tmpnam的行为

你的系统:


------------------


#include< stdio.h>


char t [L_tmpnam];


int main()

{

char s [L_tmpnam];

char * i;

char * p;


printf("%s \ n",i = tmpnam(NULL));

printf(" i =%p\\\
\ n",i);


printf("%s\ n",p = tmpnam(t));

printf("%s \ n",i) );

printf(" t = %p \ n",t);

printf(" p =%p\\\
\ n",p);


printf ("%s\ n",p = tmpnam(s));

printf("%s \ n",i);

printf( " s =%p \ n",s);

printf(" p =%p \ n",p);


返回(0);

}


------------------


我在系统上得到的输出是:


/var/tmp/tmp.0.001345

i = 0x80009078


/var/tmp/tmp.1.001345

/var/tmp/tmp.0.001345

t = 0x20a0

p = 0x20a0


/var/tmp/tmp.2.001345

/var/tmp/tmp.0.001345

s = 0xbffff5f8
p = 0xbffff5f8

马克


What''s the difference between

char buf[FILENAME_MAX];
strcpy(buf, tmpnam(NULL));

and
char buf[FILENAME_MAX];
tmpnam(buf);

According to my docs both calls write to an internal static buffer. What''s
the use of the parameter then (or the internal static buffer)?

解决方案

in comp.lang.c i read:

What''s the difference between

char buf[FILENAME_MAX];
strcpy(buf, tmpnam(NULL));

and
char buf[FILENAME_MAX];
tmpnam(buf);

According to my docs both calls write to an internal static buffer. What''s
the use of the parameter then (or the internal static buffer)?



think of it as saving you a strcpy. this can be handy in that tmpnam is
allowed to fail, in which case it returns a null pointer which makes your
first example have undefined behavior.

also, the standard doesn''t say that an internal static buffer must be used
if a non-null pointer is provided. in fact it implies such a buffer need
not be used.

--
a signature


"Serve Laurijssen" <cs@nospam.comp.com> wrote:

What''s the difference between

char buf[FILENAME_MAX];
strcpy(buf, tmpnam(NULL));

and
char buf[FILENAME_MAX];
tmpnam(buf);
In effect, nothing.

Note, however, that for a filename generated by tmpnam() you should use
L_tmpnam, not FILENAME_MAX.
According to my docs both calls write to an internal static buffer. What''s
the use of the parameter then (or the internal static buffer)?



Your docs are wrong. Only the first call writes to the static buffer;
you then copy it to your own buffer by hand. The second call writes
directly to your buffer. The end result, in both cases, is that the
filename ends up in your buffer; the second case is likely, though not
guaranteed, to be slightly faster.

Richard


Serve Laurijssen wrote:

What''s the difference between

char buf[FILENAME_MAX];
strcpy(buf, tmpnam(NULL));

and
char buf[FILENAME_MAX];
tmpnam(buf);

According to my docs both calls write to an internal static buffer. What''s
the use of the parameter then (or the internal static buffer)?


The first form

char *p = tmpnam(NULL);

creates a string that is not the name of an existing file, and returns a
pointer to an internal static array containing the string.

The second form

char s[L_tmpnam];
char *p = tmpnam(s);

stores the string in "s" and returns a pointer to the character array
"s[]". In other words, "p" will be pointing to "s[]", not to the
internal static array.

The internal static buffer plays no observable role when using the
second form.

Some programmers may consider the first form more convenient, since
there''s no need to define, and properly size, a character array before
calling tmpnam(NULL).

You can examine tmpnam''s behavior by running the following program on
your system:

------------------

#include <stdio.h>

char t[L_tmpnam];

int main()
{
char s[L_tmpnam];
char *i;
char *p;

printf("%s\n", i = tmpnam(NULL));
printf("i = %p\n\n", i);

printf("%s\n", p = tmpnam(t));
printf("%s\n", i);
printf("t = %p\n", t);
printf("p = %p\n\n", p);

printf("%s\n", p = tmpnam(s));
printf("%s\n", i);
printf("s = %p\n", s);
printf("p = %p\n", p);

return(0);
}

------------------

The output I get on my system is:

/var/tmp/tmp.0.001345
i = 0x80009078

/var/tmp/tmp.1.001345
/var/tmp/tmp.0.001345
t = 0x20a0
p = 0x20a0

/var/tmp/tmp.2.001345
/var/tmp/tmp.0.001345
s = 0xbffff5f8
p = 0xbffff5f8
Mark


这篇关于tmpnam问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆